应该是一个简单的问题(我自己教我自己的目标C)。
我想向一个返回不同对象(Waste)的对象(Cell)发送一个方法,然后删除接收者(Cell)。以下是我到目前为止所看到的内容:
#import "Cell.h"
#import "Waste.h"
@implementation Cell
@synthesize.......
other methods.....
- (Waste *) die // Send a method to an instance of class "Cell", causing a new object of class "Waste" to be made, then causing the Cell instance to "die"
{
Waste *newWaste = [[Waste alloc] init]; // Create a new object, "newWaste", of class Waste ARC Semantic Issue: No known class method for selector 'alloc'
newWaste.wasteEnergy = (0.1 * cellEnergy); // Set the energy of "newWa" to 10% what the Cell's energy is
newWaste.wasteXCoordinate = cellXCoordinate; // Set the X coordinate of "r" to the Cell's X coordinate
newWaste.wasteYCoordinate = cellYCoordinate; // Set the Y coordinate of "r" to the Cell's Y coordinate
newWaste.wasteExcreted = NO; // Variable saying if the Waste is to be excreted set to "NO"
return newWaste; // Return the new waste object
self = nil; // Have the Cell "die" ARC Semantic Issue: Cannot assign to 'self' outside of a method in the init family
};
我已经提出了评论背后的两个问题,因此您知道问题出在哪里,但它们是: ARC语义问题:没有已知的选择器'alloc'的类方法 和 ARC语义问题:无法在init系列中的方法之外分配给“self”
有人能告诉我为什么会这样,以及如何解决这个问题?我以为这很简单: Class * newInstance = [[Class alloc] init]; 并且这可以在方法内完成,因此您可以返回实例。我读过那个自我=零;是使用ARC释放对象的正常方法。
感谢您的帮助!