0

大家好,我很难理解如何使用指针(我第一次使用它们)希望有人能指出我正确的方向(没有双关语):P(它控制声音、页码、bg、音频 int , 和要显示的文本) (NSString *) textFromPlist指针,它指向包含@"Page01Text"文本的 plist 我想采用@"Page01Text"并在HighLighter 类中使用它

我能够在 hello world 层中记录声音、页码、bg、音频 int 和文本等的输出(它记录了TextManagerWithpageNum),所以我知道 plist 正在读取,但由于某种原因我无法链接textFromPlist 我'我希望能够使用textFromPlist来自TextManagerWithpageNum类并让 hilighter 类使用它(这个类将高亮文本并使用 plist 提供的音频间隔)它返回的这段代码(NULL)

    @interface HighLighter.h

    TextManagerWithpageNum *myClassAccess; 
    //declaring the TextManagerWithpageNum class so I can access textFromPlist


    @implementation HighLighter.m
     -(NSString *)Sentance01 
    {
    NSString *textFromTextManager = myClassAccess.textFromPlist;
    NSString *storyText = [NSString stringWithFormat:@"%@",textFromTextManager];
        return [[[NSString alloc]initWithString:StoryText]autorelease]; 
        //doesn't crash but doesn't load text

    NSLog(@"text form plist @%",storyText);

    }

我做错了什么?(我有一个我也希望链接的 int 数组,但我想我会从 开始,NSString因为我认为这会更简单。)任何帮助都会很棒,亲切的问候,娜塔莉。

    @interface TextManagerWithpageNum : CCLayer
    NSString* textFromPlist_Pntr;
    @property(nonatomic, readonly) NSString* textFromPlist;
   //NSString* textFromPlist is an item on the plist  @"page01"  which contains text for  the page

   @property(nonatomic, readonly) NSString* audioInterval;
   //NSString* AudioInterval is an array on the plist  @"AudioTimings_PG01"

   @implementation TextManagerWithpageNum
   @synthesize textFromPlist = textFromPlist_Pntr;
   @synthesize audioInterval = _audioInterval;
4

1 回答 1

1

在类之间传递任何数据时,有几个选项。我将使用从to发送的示例SomeData对象来展示两者。您还可以使用类级别的方法,例如,在指向单例对象的指针中发送或接收数据。我不会向您展示如何做到这一点,因为这听起来不适合您的问题。ObjectAObjectB

选项1

在 中定义接收消息ObjectB

@interface ObjectA
{
    SomeData *someDataPointer;
    ObjectB *someObjectB;
}
@end

@implementation ObjectA

-(void)someMethod
{
    someDataPointer = [[SomeData alloc] init]; // Or whatever way you want to create and set someDataPointer
    [someObjectB receiveData:someDataPointer];

    // Note: if you will be calling receiveData on someObjectB much later than the
    //    time of creation of someDataPointer, you should retain someDataPointer to
    //    hold onto it until you need to pass it on.
    //    ie)
    // [someDataPointer retain];
}

@end

@interface ObjectB
-(void)receiveData:(SomeData *)dataPtr;
@end

@implementation ObjectB

-(void)receiveData(SomeData *)dataPtr
{
    // Here the data from someDataPointer will also be pointed to by dataPtr
    // Use dataPtr now
    [self doSomethingWithData:dataPtr];
}

@end

选项 2

在 中定义发送消息ObjectA

@interface ObjectA
{
    SomeData *someDataPointer;
}

@property (nonatomic, retain) SomeData *someDataPointer;

@end

@implementation ObjectA
@synthesize someDataPointer = someDataPointer_;
@end

@interface ObjectB
{
    ObjectA *someObjectA;
}
@end

@implementation ObjectB

-(void)someMethod
{
    SomeData *dataPtr = [someObjectA getSomeDataPointer];
    // Now do something with dataPtr
}

@end

我希望这会有所帮助。我想一般性地向您展示这两种技术,以便您可以学习将其具体应用于您的案例。如果您有任何问题,请告诉我。

于 2013-08-19T22:43:58.793 回答