<分区>
标签 iphone objective-c ios
<分区>
我昨天参加了一次工作面试,他们说列出这段代码的所有错误: 认为我得到了最多但不确定...
MyView.h:
#import "UIKit.h"
@interface MyView : UIView {
NSString* name;
UIButton* button;
}
@property(assign, copy) NSString* dateString;
@property(nonatomic, retain) UIButton* button;
- (void)buttonPressed;
@end
MyView.m:
#import "MyView.h"
@implementation MyView
@synthesize dateString, button;
- (id)init {
self = [super init];
if (self != nil) {
button = [UIButton initWithFrame:CGRectMake(0, 0, 50, 50)];
[button setImage:[UIImage imageNamed:@"buttonImage.png"] forState: UIControlStateNormal];
[button addTarget:self
action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[button release];
[self addSubview:button];
NSDateFormatter *headerDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[headerDateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[headerDateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
[self setDateString:[headerDateFormatter stringFromDate: [NSDate date]]];
[headerDateFormatter release];
for(NSInteger i=0; i<=10; i++){
NSLog(@"i = ", i);
}
}
-(void)buttonPressed {
NSLog(dateString);
}
- (void)dealloc {
[button removeFromSuperView];
[button release];
[dateString release];
[super dealloc];
}
回答:
==========
[headerDateFormatter 发布];
不需要,因为已经自动发布
==========
NSLog(@"i = ", i);
应该是
NSLog(@"i = %d", i);
==========
[button release];
[self addSubview:button];
错误的方法
[self addSubview:button]; //addSubView retains button
[button release]; //so release after
=========
NSLog(dateString);
NSLog(@"%@",dateString);
=========
- (void)dealloc {
[button removeFromSuperView]; //unnecessary - whole view is about to be dealloc'd no need to break it apart
=========
你也可以为按钮设置其他状态的图像,你只为一个状态设置它
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateHighlighted];
[button setTitle:title forState:UIControlStateSelected];
=========
- (void)buttonPressed {
Event handler
- (void)buttonPressed:(UIButton *)button {
最佳答案
很多地方都错了...
#import "UIKit.h"
应该是#import <UIKit/UIKit.h>
@interface MyView : UIView {
NSString* name;
UIButton* button;
}
@property(assign, copy) NSString* dateString;
应该是(nonatomic, copy)
@property(nonatomic, retain) UIButton* button;
- (void)buttonPressed;
@end
其余的看起来很合理。
#import "MyView.h"
@implementation MyView
@synthesize dateString, button;
- (id)init {
self = [super init];
这应该调用指定的初始化程序:initWithFrame:
if (self != nil) {
button = [UIButton initWithFrame:CGRectMake(0, 0, 50, 50)];
这可能应该是 self.button = ...
;
此外,没有 +initWithFrame:
方法。这是-initWithFrame:
.
[button setImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[button release];
不应释放该按钮,因为我们没有对它的拥有引用。如果我们使用 self.button = ...
上面,那么我们还是不会在这里释放它,因为该属性被声明为retain
.
[self addSubview:button];
NSDateFormatter *headerDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[headerDateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[headerDateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
[self setDateString:[headerDateFormatter stringFromDate: [NSDate date]]];
[headerDateFormatter release];
额外的release
格式化程序上的错误,因为我们 autorelease
已经可以了
for(NSInteger i=0; i<=10; i++){
NSLog(@"i = ", i);
这缺少 %d
(或 %i
或其他)
}
}
return self;
语句丢失。
-(void)buttonPressed {
NSLog(dateString);
你不应该直接记录字符串。这应该是 NSLog(@"%@", dateString);
}
- (void)dealloc {
[button removeFromSuperView];
这是不必要的,但放在这里也很好。
[button release];
这在技术上是不正确的,因为我们从未保留 button
在初始化程序中。但是,如果我们修复初始化程序以正确保留按钮,那么这是正确的。
[dateString release];
[super dealloc];
}
另外,name
从未使用过 ivar。
关于iphone - Objective-C考试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7250070/