0

好吧,我有 2 个班级,想将 NSString 从 class2 获取到 class1。

我哪里错了?

类1.h

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

类1.m

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

类2.h

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

类2.m

#import "class2.h"
@synthesize astring;
@implementation class2
-(NSString)getstring {
    return [self astring];
}
@end
4

2 回答 2

2

检查你的一些表达。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-04T06:06:15.090 回答
0

您需要返回一个指向NSString

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

和:

-(NSString*)getstring;
于 2012-04-04T06:05:28.927 回答