我试图分开alloc和initWithRed …消息,但它似乎不工作

对不起,如果它被问到的地方,但作为一个初学者,我需要一个非常具体的答案我的问题。 错在哪里,更正和build议。

我写下那些申请didFinishLaunchingWithOption:

UIColor *myBackgroundColor = [[UIColor alloc]initWithRed:.87 green:.77 blue:.56 alpha:.99]; [window setBackgroundColor:myBackgroundColor]; 

它工作,并改变背景的颜色,然后我试图分开这两个消息。

 UIColor *myBackgroundColor = [UIColor alloc]; [myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99] [window setBackgroundColor:myBackgroundColor]; 

我应该如何编码才能使其正确运行? 我将需要理由和更正。 非常感谢。

你不能假定allocinit具有相同的返回值。

以下应该工作:

 UIColor *myBackgroundColor = [UIColor alloc]; myBackgroundColor = [myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99] [window setBackgroundColor:myBackgroundColor]; 

我不明白你为什么要添加额外的行。

使用…

 [UIColor colorWithRed:0.87 green:0.77 blue:0.56 alpha:0.99];