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

ドット表記は、 type のオブジェクトのメソッドを呼び出しませんid

その配賦ラインを次のように変更します。

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

isSubclassOfClass:条件付きであるため、上記のキャストは正確に正しいことに注意してください。

于 2013-07-12T21:33:08.367 に答える