当我运行此代码时,控制台中没有任何内容。 print方法应显示NSLog()
的内容,但不显示。这是一个单一的视图应用程序。
Person.h:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
int age;
int weight;
}
-(void) print;
-(void) myAge: (int) theAge;
-(void) myWeight: (int) theWeight;
@end
Person.m:
#import "Person.h"
@implementation Person
-(void) print {
NSLog(@"My Age is %i and my weight is %i", age, weight);
}
-(void) myAge: (int) theAge {
age = theAge;
}
-(void) myWeight: (int) theWeight {
weight = theWeight;
}
@end
的main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Person.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
Person *Connor = [[Person alloc] init];
[Connor myAge: 20];
[Connor myWeight:210];
[Connor print];
}
答案 0 :(得分:2)
对UIApplicationMain
的调用永远不会返回,因此之后的代码永远不会运行。
在调用UIApplicationMain
之前将代码移至。
为什么不使用age
和weight
的属性?
Person.h:
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int weight;
-(void) print;
@end
Person.m:
#import "Person.h"
@implementation Person
- (void) print {
NSLog(@"My Age is %i and my weight is %i", self.age, self.weight);
}
@end
的main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Person.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
Person *connor = [[Person alloc] init];
connor.age = 20;
connor.weight = 210;
[connor print];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}