Как вы оцениваете ценности между классами в target-c? - PullRequest
0 голосов
/ 03 августа 2010

Как вы проходите значения между классами в target-c?

1 Ответ

2 голосов
/ 03 августа 2010

Я собираюсь предположить, что вопрос касается класса ClassOne с переменной экземпляра int integerOne, к которой вы хотите получить доступ из другого класса ClassTwo. Лучший способ справиться с этим - создать свойство в ClassOne. В ClassOne.h:

@property (assign) int integerOne;

Это объявляет свойство (в основном, два метода, - (int)integerOne и - (void)setIntegerOne:(int)newInteger). Тогда в ClassOne.m:

@synthesize integerOne;

Это "синтезирует" два метода для вас. Это в основном эквивалентно:

- (int)integerOne
{
    return integerOne;
}

- (void)setIntegerOne:(int)newInteger
{
    integerOne = newInteger;
}

Теперь вы можете вызывать эти методы из ClassTwo. В ClassTwo.m:

#import "ClassOne.h"
//Importing ClassOne.h will tell the compiler about the methods you declared, preventing warnings at compilation

- (void)someMethodRequiringTheInteger
{
    //First, we'll create an example ClassOne instance
    ClassOne* exampleObject = [[ClassOne alloc] init];

    //Now, using our newly written property, we can access integerOne.
    NSLog(@"Here's integerOne: %i",[exampleObject integerOne]);

    //We can even change it.
    [exampleObject setIntegerOne:5];
    NSLog(@"Here's our changed value: %i",[exampleObject integerOne]);
}

Похоже, вы должны пройти несколько уроков, чтобы изучить эти понятия Objective-C. Я предлагаю эти .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...