2012-04-23 18 views
6

Próbuję przekonwertować const char * na NSString *, a następnie przekonwertować go z powrotem. To działa, ale ja dostać:Konwertuj const char * na NSString * i przekonwertuj - _NSAutoreleaseNoPool()

__NSAutoreleaseNoPool(): Object 0x100550a40 of class NSCFArray autoreleased with no pool in place - just leaking 
__NSAutoreleaseNoPool(): Object 0x100551730 of class NSCFString autoreleased with no pool in place - just leaking 
__NSAutoreleaseNoPool(): Object 0x100551f10 of class NSCFData autoreleased with no pool in place - just leaking 

Kod jest:

const char* convert = "hello remove this: *"; 

NSString *input = [[NSString alloc] initWithUTF8String:convert];// convert 

//remove * FROM THE STRING   
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; 

// REPLACE * WITH NOTHING     
input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; 

// CONVERT BACK   
const char *converted_back = [input UTF8String]; 

jestem zagubiony, proszę mi pomóc.

+0

Pokaż cały twój kod, myślę, że wiem, jaki jest problem, ale najpierw muszę sprawdzić cały twój kod. –

Odpowiedz

15

Jeśli robisz to w wątku tła, dodaj NSAutoReleasePool.

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
const char* convert = "hello remove this: *"; 
NSString *input = [[[NSString alloc] initWithUTF8String:convert] autorelease];// convert 
//remove * FROM THE STRING   
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; 
// REPLACE * WITH NOTHING     
input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; 
// CONVERT BACK   
const char *converted_back = [input UTF8String]; 
[pool drain]; 

Ponadto, trzeba zwolnić input po skończysz z nim, albo zrobić to autoreleased.

+0

robię to w pliku nagłówkowym, a następnie włączam/importuję to wewnątrz soku NSApplicationDelegate, czy mógłbyś pokazać mi przykład, ponieważ jestem nowy w tym ... – user1341993

+0

im otrzymuję sygnał odbieranego programu: "EXC_BAD_ACCESS". – user1341993

+0

@ user1341993 - patrz edytuj – MByD