【发布时间】:2014-04-12 23:25:42
【问题描述】:
我是 iOS 编程的新手,我刚刚构建了一个 iPhone 应用程序,它可以向用户提问并返回答案。构建环境为 OS X 10.9 和 Xcode 5.0.2。每次我启动 iPhone 模拟器时,Debug Navigator 都会显示内存使用量为 13.5mb,但即使在我返回主屏幕后它也会继续上升。一分钟后,内存使用量将稳定在 17.5mb 左右。这是正常行为还是我需要添加一些内存管理代码?
#import "QuizViewController.h"
@interface QuizViewController ()
@property (nonatomic) int currentQuestionIndex;
@property (nonatomic, copy) NSArray *questions;
@property (nonatomic, copy) NSArray *answers;
@property (nonatomic,weak) IBOutlet UILabel *questionLable;
@property (nonatomic,weak) IBOutlet UILabel *answerLable;
@end
@implementation QuizViewController
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
self.questions = @[@"From what is cognac made?",
@"What is 8 + 8 ?",
@"What is the capital of Minnesota?"];
self.answers = @[@"Grapes",
@"16",
@"St.Paul"];
}
return self;
}
- (IBAction)showQuestion:(id)sender
{
self.currentQuestionIndex++;
if (self.currentQuestionIndex == [self.questions count]){
self.currentQuestionIndex = 0;
}
NSString *question = self.questions[self.currentQuestionIndex];
self.questionLable.text = question;
self.answerLable.text = @"???";
}
- (IBAction)showAnswer:(id)sender
{
NSString *answer = self.answers[self.currentQuestionIndex];
self.answerLable.text = answer;
}
@end
【问题讨论】:
-
为什么你的 UIViewController 初始化方法使用“
- (instancetype)”而不是“- (id)”? -
如果内存使用稳定,那么你可能没问题。如果在您在控制器之间来回切换时它一直在上升,那么您做错了。
-
Michael,这个链接说使用 instancetype 比使用 id 更好。 stackoverflow.com/questions/8972221/…
标签: ios iphone objective-c xcode memory-management