Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 03, 2014, 12:00:53 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Recent Posts
Pages: 1 2 [3] 4 5 ... 10

 21 
 on: August 20, 2014, 03:24:34 AM 
Started by jeffreyho - Last post by jeffreyho
Hi, I am in chapter 8 now, and I learn Objective-C because I want to make iOS app. So, should I keep learning Objective-C if there will be Swift soon?   Undecided

 22 
 on: August 15, 2014, 12:37:40 AM 
Started by 0NobHut - Last post by mitchb
The exercise was to convert -8645 to 5468- so this is how I did it.
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        int number, right_digit, reverseNum;
        BOOL isNegative = NO;
       
        NSLog(@"Enter your number.");
        scanf("%i", &number);
       
        if ( number < 0 )
        {
            number = -number;
            isNegative = YES;
        }
       
            // Reverse the number
        do
        {
            right_digit = number % 10;
            reverseNum = reverseNum * 10 + right_digit;
            number /= 10;
        } while ( number != 0 );
       
        if (isNegative == YES)
            NSLog(@" %i-", reverseNum);
       
        else
            NSLog(@" %i", reverseNum);
    }
    return 0;
}

Mitch

 23 
 on: August 14, 2014, 08:33:03 PM 
Started by 0NobHut - Last post by gosborne3
Just got through this myself, didn't have much of an idea when I started but it came together after a few tries at converting the input into a string LOL

       int number, right_digit;
        NSLog (@"Enter your number.");
        scanf("%i", &number);
        if (number < 0){
            number = number * -1;
            do{
            right_digit = number % 10;
            NSLog (@"%i", right_digit);
            number /= 10;
               
            }
            while (number !=0);
       
                NSLog(@"-");
        }
            else {
                do{
                    right_digit = number % 10;
                    NSLog (@"%i", right_digit);
                    number /= 10;
                   
                }
                while (number !=0);
               
            }
       
       

 24 
 on: August 05, 2014, 07:43:46 PM 
Started by henrydang - Last post by henrydang
Hi everyone,
Here's again my full question: "How can main method directly access the instance variable origin of the Rectangle class to change its value?"

There is other class (XYPoint) but I omit it.

//---------Rectangle.m-------------------------------
#import "Rectangle.h"

@implementation Rectangle
{
   XYPoint *origin;
}

@synthesize width, height;

-(void) setWidth: (int) w andHeight: (int) h
{
   width = w;
   height = h;
}

(void) setOrigin: (XYPoint *) pt
{
    origin = pt;
}

(int) area
{
    return width * height;
}

(int) perimeter
{
    return (width + height) * 2;
}

(XYPoint *) origin
{
    return origin;
}
@end

//----------------main---------------------------
#import "Rectangle.h"
#import "XYPoint.h"

int main (int argc, char * argv[])
{
   @autoreleasepool {
      Rectangle *myRect = [[Rectangle alloc] init];
      XYPoint   *myPoint = [[XYPoint alloc] init];

      [myPoint setX: 100 andY: 200];

      [myRect setWidth: 5 andHeight: 8];
      myRect.origin = myPoint;       //Is this the same as [myRect origin]? This is my confusion!!!!!!!!!!

      NSLog (@"Rectangle w = %i, h = %i", myRect.width, myRect.height);

      NSLog (@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);

      NSLog (@"Area = %i, Perimeter = %i",
         [myRect area], [myRect perimeter]);
   }
   return 0;
}

I run this and it works as expected (i.e: the origin variable of myRec is set to newly allocated myPoint object)

Any help would be appreciated. I'm new to Objective-C



 25 
 on: August 04, 2014, 02:13:57 PM 
Started by 星如雨 - Last post by mitchb
I would like to point out that @property, @synthesize, and a method taking multiple arguments is not presented until chapter 7. A beginner at this point in the book will not understand your code.

Part of this exercise to write your own accessors, not synthesize them. Another thing is even after synthesizing the accessors you wrote -(int)width; and -(int)length;. These getters were already synthesized and the compiler should have generated a warning.

Other then the comments above your program looks fine, keep up the good work.

Mitch

 26 
 on: August 03, 2014, 11:10:15 PM 
Started by 星如雨 - Last post by 星如雨
// Exerise 7 Rectangle
#import <Foundation/Foundation.h>

@interface Rectangel : NSObject

@property (nonatomic)  int length, width;
-(void) setLength: (int) l setWidth : (int) w;
-(int) length;
-(int) width;
-(int) area;
-(int) perimeter;

@end

@implementation Rectangel

@synthesize length, width;
-(void) setLength:(int)l setWidth:(int)w
{
    length = l;
    width = w;
}
-(int) length
{
    return length;
}
-(int) width
{
    return  width;
}
-(int) area
{
    return length*width;
}
-(int) perimeter
{
    return (length+width)*2;
}

@end

int main(int argc, const char *argv[])
{
    @autoreleasepool {
        Rectangel *testRect = [[Rectangel alloc]init];
        [testRect setLength:123 setWidth:456];
       
        NSLog(@"This rectangel length: %i", [testRect length]);
        NSLog(@"This rectangel width: %i", [testRect width]);
        NSLog(@"Area = %i", [testRect area]);
        NSLog(@"Perimeter = %i", [testRect perimeter]);
    }   
}

 27 
 on: August 03, 2014, 08:43:22 PM 
Started by 星如雨 - Last post by 星如雨
// Exerise 6 Complex a+bi
#import <Foundation/Foundation.h>

@interface Complex : NSObject

@property (nonatomic)  double real, imaginary;
-(void) print;
-(void) setReal:(double) x setImaginary:(double) y;
-(double) real;
-(double) imaginary;

@end


@implementation Complex

@synthesize real, imaginary;
-(void) print
{
    NSLog(@"%.2f + %.2fi", real, imaginary);
}
-(void) setReal:(double)x setImaginary:(double)y
{
    real = x;
    imaginary = y;
}
-(double) real
{
    return real;
}
-(double) imaginary
{
    return imaginary;
}

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        // insert code here...
        Complex *testComplex = [[Complex alloc]init];
        [testComplex setReal:3.3 setImaginary:4.7];
        //[testComplex print];
        NSLog(@"Complex is %.2f + %.2fi", [testComplex real], [testComplex imaginary]);
       
    }
    return 0;
}

 28 
 on: August 03, 2014, 04:38:36 PM 
Started by jonr - Last post by BrianLawson
Thanks for the clarification Mitch.

 29 
 on: August 03, 2014, 04:31:32 PM 
Started by jonr - Last post by mitchb
In the sixth edition review pages 39 through 43 where Stephen discusses this subject. I would also recommend reading the Apple documentation "Programming With Objective-C". Apple states that "It's possible for init to return a different object than was created by alloc, so it's best practice to nest the calls as shown." What is shown is: NSObject *newObject = [[NSObject alloc] init];. Again from the Apple doc "Never initialize an object without reassigning any pointer to that object. As an example, don't do this: NSObject *someObject = [NSObject alloc];  [someObject init];.  If the call to init returns some other object, you'll be left with a pointer to the object that was originally allocated but never initialized."

Mitch

 30 
 on: August 03, 2014, 02:07:09 PM 
Started by jonr - Last post by BrianLawson
Hi Jon,

You've got it mostly right. Where you got off the trail is that the init does not change the contents of the pointer. In statement 4 where you call alloc without the init you get a chunk of memory assigned but not inited for the object you've created. Then in statement 7 where you call alloc with an init you are requesting a new chunk of memory for a second object which is also inited. This is why the contents of myTest2 change. If statement 7 was changed to [myTest2 init]; you'd see that the contents of myTest2 remain the same but the memory it points to changes because it has been inited.

Pages: 1 2 [3] 4 5 ... 10


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.