这是来自Apple的iPhone"Utility Aplication"模板的未经修改的代码:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}
Run Code Online (Sandbox Code Playgroud)
当mainViewController
被分配到aController
时,self
被指定的关键字:
self.mainViewController = aController;
Run Code Online (Sandbox Code Playgroud)
但是,mainViewController
设置框架时,self
不需要关键字:
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
Run Code Online (Sandbox Code Playgroud)
如果我self
从第一个示例中删除关键字,程序将崩溃并显示以下消息:
objc[1296]: FREED(id): message view sent to freed object=0x3b122d0
Run Code Online (Sandbox Code Playgroud)
如果我将self
关键字添加到第二个示例,程序运行正常.
任何人都可以解释为什么self
在第一种情况下需要而不是第二种情况?我假设在两种情况下mainViewController
都引用相同的实例变量.
我试图将UIlabel中的文本颜色与UIColor进行比较,但结果始终为false.
以下代码生成结果:
color equal 1, 0
Run Code Online (Sandbox Code Playgroud)
我希望a和b都等于1.还有另一种方法可以做比较吗?
bool a,b;
UIColor *myColor1, *myColor2;
myColor1 = [UIColor redColor];
mainViewController.timerLabel.textColor = [UIColor redColor];
myColor2 = [UIColor colorWithCGColor:mainViewController.timerLabel.textColor.CGColor];
a = [[UIColor redColor] isEqual:myColor1];
b = [[UIColor redColor] isEqual:myColor2];
NSLog(@"color equal %i, %i",a,b);
Run Code Online (Sandbox Code Playgroud) 我正在我的iphone应用程序中构建一个字典数组(称为键)来保存tableview的节名和行数.
代码如下所示:
[self.results removeAllObjects];
[self.keys removeAllObjects];
NSUInteger i,j = 0;
NSString *key = [NSString string];
NSString *prevKey = [NSString string];
if ([self.allResults count] > 0)
{
prevKey = [NSString stringWithString:[[[self.allResults objectAtIndex:0] valueForKey:@"name"] substringToIndex:1]];
for (NSDictionary *theDict in self.allResults)
{
key = [NSString stringWithString:[[theDict valueForKey:@"name"] substringToIndex:1]];
if (![key isEqualToString:prevKey])
{
NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:i],@"count",
prevKey,@"section",
[NSNumber numberWithInt:j],
@"total",nil];
[self.keys addObject:newDictionary];
prevKey = [NSString stringWithString:key];
i = 1;
}
else
{
i++;
}
j++;
}
NSDictionary *newDictionary = [NSDictionary …
Run Code Online (Sandbox Code Playgroud)