0

希望应该是一个简单的问题(在我去的时候自学Objective C)。

我想向一个对象(Cell)发送一个方法,该对象返回一个不同的对象(Waste),然后删除接收者(Cell)。到目前为止,这是我所看到的:

#import "Cell.h"
#import "Waste.h"

@implementation Cell
@synthesize.......
other methods.....

// 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 *) die {
    // Create a new object, "newWaste", of class Waste
    // ARC Semantic Issue: No known class method for selector 'alloc'
    Waste *newWaste = [[Waste alloc] init];
    // Set the energy of "newWa" to 10% what the Cell's energy is
    newWaste.wasteEnergy = (0.1 * cellEnergy);

    // Set the X coordinate of "r" to the Cell's X coordinate
    newWaste.wasteXCoordinate = cellXCoordinate;

    // Set the Y coordinate of "r" to the Cell's Y coordinate
    newWaste.wasteYCoordinate = cellYCoordinate;

    // Variable saying if the Waste is to be excreted set to "NO"
    newWaste.wasteExcreted = NO;

    // Return the new waste object
    return newWaste;

    // Have the Cell "die"  ARC Semantic Issue: 
    // Cannot assign to 'self' outside of a method in the init family
    self = nil;
}

我已将出现的两个问题放在评论后面,以便您知道问题出在哪里,但它们是: ARC Semantic Issue: No known class method for selector 'alloc'ARC Semantic Issue: Cannot assign to 'self'在 init 系列中的方法之外

有人能告诉我为什么会发生这种情况以及如何解决吗?我原以为这样做很简单: Class *newInstance = [[Class alloc] init]; 并且这可以在方法内部完成,以便您可以返回实例。我读过 self = nil; 是使用 ARC 释放对象的正常方式。

4

0 回答 0