Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 09:44 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 3
Shwaa is on a distinguished road
Default X-Code warnings

Forgive me if this is not the best place to post this question. I am working my way through the Big Nerd Ranch iPhone Programming Guide, and following the code along as I go. I am stuck on Chapter 2 (yes already)

I am getting 4 warnings on my possession.m file and it won't let me compile and run it. I am attaching the .m file where the warnings are occurring. I have commented the warnings I am receiving in all CAPS.

If I need to attach the other 2 files (main.m and possession.h) let me know, although it doesn't seem like these warnings are related to the other files. Any help is appreciated....

Code:
#import "Possession.h"

@implementation Possession  //***INCOMPLETE IMPLEMENTATION***
@synthesize possessionName, serialNumber, valueInDollars, dateCreated;


- (id)initWithPossessionName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber

{
    // Call the superclass's designated initializer
    self =[super init];
    
    // Did the supeclass's designated initializer fail?
    if (!self) 
        return nil;
        
    
    
    
    // Give the instance variable initial values
    [self setPossessionName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    
    //Return the address of the newly initialized object
    
    return self;
                            
}

- (NSString *) description
{
    NSString * descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, Recorded on %@", 
     possessionName,
     serialNumber,
     valueInDollars,
     dateCreated];
    
    return descriptionString;
}

- (id)init

{
    return [self initWithPossessionName:@"Possession"
                         valueInDollars:0
                           serialNumber:@""];
}

+ (id) randomPossession

{
    // Create two arrays with a list of possible adjectives and nouns
    // Note: When using NSArray's arrayWithObjects:, you can pass as many
    // objects as you like.  At the end of that list, you put nil to
    // signifify that there are no more objects - otherwise you will crash.
    // The nil value is not added to the array, but is used by the method
    // to determine the end of the list.
    
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",
                                    @"Rusty",
                                    @"Shiny", nil];
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
                               @"Spork",
                               @"Mac", nil];
    
    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder.  So adjectiveIndex is a random number
    // from 0 to 2 inclusive, in this case.
    
    int adjectiveIndex =random() % [randomAdjectiveList count];  //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***
    int nounIndex = random() % [randomNounList count];           //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***   
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            [randomAdjectiveList objectAtIndex:adjectiveIndex],
                            [randomNounList objectAtIndex:nounIndex]];
    
    int randomValue = random() % 100;                           //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***   
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10];
    
    //Once again, ignore the memory problems with this method
    // We use "self" instead of the name of the class in class methods...
    // Keep reading to find out why
    
    Possession *newPossession =
    [[self alloc] initWithPossessionName:randomName
                          valueInDollars:randomValue
                            serialNumber:randomSerialNumber];
    return newPossession;
    
    
    
}

@end
Shwaa is offline   Reply With Quote
Old 09-07-2011, 09:51 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You could find a lot by Googling those warnings. The first one says that you have methods declared in the header, but you never wrote the implementations for them. The other three are saying that the function is returning a long integer and you are storing it in an int, which has much smaller range and you can possibly overflow it, lose precision.

Also, keep in mind that random() function has to be seeded to work, otherwise you'll always get the same sequence of numbers. Try using arc4random() instead which is self-seeding and gives a much better random numbers.
baja_yu is offline   Reply With Quote
Old 09-07-2011, 10:09 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 3
Shwaa is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You could find a lot by Googling those warnings. The first one says that you have methods declared in the header, but you never wrote the implementations for them. The other three are saying that the function is returning a long integer and you are storing it in an int, which has much smaller range and you can possibly overflow it, lose precision.

Also, keep in mind that random() function has to be seeded to work, otherwise you'll always get the same sequence of numbers. Try using arc4random() instead which is self-seeding and gives a much better random numbers.
Thanks for the reply Baja....

I am still confused as to what methods I declared in the header file but never wrote the implementations for them. Here is the header file, I just double checked my work against the book...


Code:
#import <Foundation/Foundation.h>


@interface Possession : NSObject
{
    NSString *possessionName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}

@property (nonatomic, copy) NSString *possessionName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly) NSDate *dateCreated;

+ (id) randomPossession;

- (id)initWithPossessionName: (NSString *)name
              valueInDollars: (int)value
                serialNumber: (NSString *)sNumber;

-(id)initWithPossessionName:(NSString *)name;


@end
As far as the other 3 warnings goes, I am not sure why the book would say to store the result as an int, I see your point. Wouldn't the % operator bypass that and just give you the remainder though? Or is that not how it works...

Thanks again
Shwaa is offline   Reply With Quote
Old 09-08-2011, 06:35 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Code:
-(id)initWithPossessionName:(NSString *)name;
You have that method declaration in the header and it's not implemented.
baja_yu is offline   Reply With Quote
Old 09-08-2011, 08:55 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 3
Shwaa is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Code:
-(id)initWithPossessionName:(NSString *)name;
You have that method declaration in the header and it's not implemented.
Thanks again Baja. I had that in there originally but I thought the next section of the book said to overwrite that line with -(id)init, which is what I did. I must have read that wrong, I will go back and check
Shwaa is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
8 members and 378 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:55 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0