我可以从其他班级获得NSString吗?

时间:2012-04-04 06:02:45

标签: objective-c class nsstring init

好吧,我有2个类,希望从class2到class1获得NSString。

我哪里错了?

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

2 个答案:

答案 0 :(得分:2)

检查你的表情。 NSString - &gt; 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

答案 1 :(得分:0)

您需要返回指向NSString的指针:

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

-(NSString*)getstring;