目標C新手在這裏:我的int main在下面。問題:爲什麼我得到這個臭名昭着的錯誤消息:「沒有可行性@interface for person declares the selector firstName」沒有人知道@interface爲人
.m文件具有@synthesize firstName,lastName;
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Person.h"
int main(int argc, char *argv[])
{
Person *ben = [[Person alloc] init];
[email protected]"Ben";
NSLog(@"--->%@",ben.firstName); //output ---->Ben
[ben firstName:"Ben"]; //RED ERR: no visibility @interface for Person
}
的.H
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *firstName;
NSString *lastName;
}
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
- (NSString *) fullName;
-(void) sayHello;
@end
的.M
#import "Person.h"
@implementation Person
@synthesize firstName, lastName;
-(void) sayHello
{
NSLog(@"Hello");
}
- (NSString *) fullName
{
return [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
}
@end
由於「Foundation.h」爲您導入,因此不需要導入「NSObject.h」或「NSString.h」。 – Macro206 2013-05-01 19:37:55
明白了。謝謝! – adhg 2013-05-01 19:41:51