Can I get NSString from other class?

Go To StackoverFlow.com

0

well, I have 2 classes and want to get NSString from class2 to class1.

where do I wrong?

class1.h

#import <Cocoa/Cocoa.h>
#import "class2.h"
@class class2;
@interface class1 : NSObject
-(IBAction)getstringfromclass2(id)sender;
@end

class1.m

#import "class1.h"
@implementation class1
-(IBAction)getstringfromclass2(id)sender {
    class2 *controller = [[class2 alloc] init];
    NSLog(@"%@", [controller getstring]);
}
@end

class2.h

#import <Cocoa/Cocoa.h>
@interface class2 : NSObject {
    NSString *astring;
}
-(NSString)getstring;
@property (readwrite,retain) NSString *astring;
@end

class2.m

#import "class2.h"
@synthesize astring;
@implementation class2
-(NSString)getstring {
    return [self astring];
}
@end
2012-04-04 06:02
by user840250
Please show the code that you use to create these objects and what you expect to happen as well as what really happened - lnafziger 2012-04-05 05:02


2

check your some expression. NSString -> NSString *

#import <Cocoa/Cocoa.h>
@interface class2 : NSObject {
    NSString *astring;
}
-(NSString *)getstring;
@property (readwrite,retain) NSString *astring;
@end
class2.m

#import "class2.h"
@synthesize astring;
@implementation class2
-(NSString *)getstring {
    return [self astring];
}
@end
2012-04-04 06:06
by bitmapdata.com


0

You need to return a pointer to NSString:

-(NSString*)getstring {
    return [self astring];
}

And:

-(NSString*)getstring;
2012-04-04 06:05
by MByD
still astring = NULL - user840250 2012-04-04 08:14
-(void)awakeFromNib {
astring = [[NSString alloc] initWithFormat:@"teststring"]; } and still NUL - user840250 2012-04-04 08:19
awakeFromNib is Not called in your class2 because you create it programmatically. Initialize the string in the init method. And simply use self.astring =@"..... - Mario 2012-04-04 08:46
Ads