Search code examples
objective-cxcodensstringtitaniumcgpath

Why does my NSString becomes CGPath? Xcode, Titanium


So the short of it is I want to define a global string variable that I can reference whenever. The function that I reference it in, it returns a string. As soon as I store it and reference it in another function it outputs as <CGPath 0x5bbf50>

What the heck? The code is below and keep in mind this is a module for Titanium. First, the definition of the global variable..

@interface ComTestModule : TiModule <CBCentralManagerDelegate, CBPeripheralDelegate>
{
    NSString * teststring;
}

The next part is the function where I first send the string variable from titanium to xcode..

-(void)setService:(id)args{
    ENSURE_ARG_COUNT(args, 2);
    teststring = [args objectAtIndex:0];
    NSLog(teststring);
}

The output of the NSLog displays the actual string that was passed. Now the final function where I call the string again and attempt to output it to the log..

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
        NSLog(@"---%@", teststring);
}

As I said before, during this step it outputs as ---<CGPath 0x3ef4e0>

I'm really not sure what's going on.. Any help at all about getting this to return as the original string instead of the CGPath would be great!


Solution

  • Effects like this typically happen when you store a pointer to an object that was released and deallocated. The runtime will then replace that chunk of memory that previously held an NSString instance with, in this particular case, a CGPath instance.

    If you want to ensure that your NSString stays alive, you need to take ownership of it. You can do that by either retaining it or copying it. Copying is the preferred method when talking about strings, so try replacing this line:

    teststring = [args objectAtIndex:0];
    

    With this:

    teststring = [[args objectAtIndex:0] copy];
    

    Now just be sure to release it when you're done.