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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2010, 09:37 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 2
Default Newbie question about strings

Hello - I declared a class which has an NSString object as instance variable. As an excercise, I would like to create an instance of the class and set the variable, but I am completely unable to do it.
This is the horrible code I wrote... (Started Obj C yesterday night... ).

Code:
#import <Foundation/Foundation.h>

@interface Person : NSObject
  {
    NSString *person;
  }
  -(void) SetPersonName : (NSString *) name;
  //-(NSString) GetPersonName;
@end

@implementation Person
  -(void) SetPersonName : (NSString*) name;
    {
      person = &name;
    }  
@end

int main (int argc, const char * argv[])
  {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Person testperson;
    testperson = [Person alloc];
    testperson = [testperson init];

    NSString nametogive = @"Louis Armstrong";
    [testperson SetPersonName =nametogive];
    [pool drain];
    return 0;
  }
I get the following errors:

Quote:
pollux@desktop:~/Desktop$ gcc `gnustep-config --objc-flags` -lgnustep-base stringtest.m -o eloisa
stringtest.m: In function ‘-[Person SetPersonName:]’:
stringtest.m:14: warning: assignment from incompatible pointer type
stringtest.m: In function ‘main’:
stringtest.m:22: error: statically allocated instance of Objective-C class ‘Person’
stringtest.m:22: error: statically allocated instance of Objective-C class ‘Person’
stringtest.m:23: error: incompatible types when assigning to type ‘struct Person’ from type ‘id’
stringtest.m:24: error: cannot convert to a pointer type
stringtest.m:24: error: incompatible types when assigning to type ‘struct Person’ from type ‘id’
stringtest.m:26: error: invalid initializer
stringtest.m:26: error: statically allocated instance of Objective-C class ‘NSString’
stringtest.m:26: error: statically allocated instance of Objective-C class ‘NSString’
stringtest.m:27: error: expected ‘]’ before ‘=’ token
stringtest.m:27: warning: ‘Person’ may not respond to ‘-SetPersonName’
stringtest.m:27: warning: (Messages without a matching method signature
stringtest.m:27: warning: will be assumed to return ‘id’ and accept
stringtest.m:27: warning: ‘...’ as arguments.)
stringtest.m:27: error: cannot convert to a pointer type
stringtest.m:26: warning: unused variable ‘nametogive’
It's quite obvious I am doing something wrong!

Thanks!
yaxomoxay is offline   Reply With Quote
Old 02-17-2010, 09:50 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 2
Default

OK, I reply to myself. I just found a similar example and this is the result :

Code:
#import <Foundation/Foundation.h>

@interface Person : NSObject
  {
    NSString *person;
  }
  -(void) SetPersonName : (NSString*) name;
  //-(NSString) GetPersonName;
@end

@implementation Person
  -(void) SetPersonName : (NSString*) name;
    {
      [name retain];
      //[person release]
      person = name;
    }  
@end

int main (int argc, const char * argv[])
  {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Person * testperson;
    testperson = [Person alloc];
    testperson = [testperson init];

    NSString * nametogive = @"Louis Armstrong";
    [testperson SetPersonName : nametogive];
    [pool drain];
    return 0;
  }
Somehow I don't get any error, I have to verify if it actually prints "Louis Armstrong". I'll let you know. One question. I don't get this passage:
Code:
-(void) SetPersonName : (NSString*) name;
    {
      [name retain];
      //[person release]
      person = name;
    }
What is the "retain" doing ?

Thanks
yaxomoxay is offline   Reply With Quote
Old 02-23-2010, 06:52 AM   #3 (permalink)
Mobile and Web solutions
 
hello24's Avatar
 
Join Date: May 2009
Location: London, UK
Posts: 72
Send a message via Skype™ to hello24
Default

Retain releases the object - read the memory management guide from Apple.

Also, you should consider using properties - you get setters and getters for free.
hello24 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: 271
19 members and 252 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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