2

我是 Objective C 的新手,我对这个概念感到困惑。

以下方法应该创建作为参数传递的类的实例并编辑实例的变量。

- (void) createObject:(Class)objecttype atPoint: (CGPoint)position {

    if ([objecttype isSubclassOfClass:[GameObject class]]){

        id theobject = [[objecttype alloc] init];

        theobject.x = position.x; //error
        theobject.y = position.y; //error
    }
}

我在上面指出的地方收到一条错误消息: Property 'x' not found on object of type '__strong id'

看起来应该很简单,但我不明白出了什么问题。

4

1 回答 1

6

点表示法不会调用类型对象的方法id

将该分配行更改为:

GameObject *theobject = (GameObject*)[[objecttype alloc] init];

请注意,由于isSubclassOfClass:有条件,上述转换是完全正确的。

于 2013-07-12T21:33:08.367 回答