【发布时间】:2011-11-21 05:49:42
【问题描述】:
我有构建 NSString 并正确返回它的方法。 但是,如果我从单独的线程调用相同的方法,即使我的 NSString 不符合返回值,它也会返回 nil。
我在“getPhones”方法中得到 request = nil。
“createSelectPhonesRequest”中的“return request”包含有效字符串。
“getXmlRow”实际上构建了字符串。
+ (void)getUserPhones:(User *)user
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[DataManager getPhones:user];
[pool release];
}
+ (void)getPhones:(User*)user
{
NSString *request = [XMLBuilder createSelectPhonesRequest:user.NickName];
NSString *getAddressesResponce = [TrafficManager sendRequest:request];
}
+(NSString*) createSelectPhonesRequest:(NSString*)nickName
{
NSString *request;
NSMutableArray *attributes;
NSMutableArray *attributesValues;
/*** root ***/
attributes = [NSMutableArray new];
attributesValues = [NSMutableArray new];
//time
[attributes addObject:xmlAt.time];
[attributesValues addObject:[NSDate getXmlFormattedDateFromCurrentDate]];
//action
[attributes addObject:xmlAt.action];
[attributesValues addObject:xmlAV.selectPhones];
request = [XMLBuilder getXmlRow:xmlEl.envelop
attributes:attributes
attributesValues:attributesValues
value:nil
close:YES];
[attributes release];
[attributesValues release];
return request;
}
+(NSString*) getXmlRow:(NSString*)element
attributes:(NSMutableArray*)attributes
attributesValues:(NSMutableArray*)attributesValues
value:(NSString*)value
close:(BOOL)close
{
//open tag
NSString* xmlRow = @"<";
//tag
xmlRow = [xmlRow stringByAppendingString:element];
//attrivutes+values
if (attributes && attributesValues)
for (int i=0; i<[attributes count]; i++)
{
NSString *attribute = (NSString*) [attributes objectAtIndex:i];
NSString *attributeValue = (NSString*) [attributesValues objectAtIndex:i];
xmlRow = [xmlRow stringByAppendingString:@" "];
xmlRow = [xmlRow stringByAppendingString:(attribute)];
xmlRow = [xmlRow stringByAppendingString:@"=\""];
xmlRow = [xmlRow stringByAppendingString:attributeValue];
xmlRow = [xmlRow stringByAppendingString:@"\""];
}
//end tag
xmlRow = [xmlRow stringByAppendingString:@">"];
//value
if (value != nil)
xmlRow = [xmlRow stringByAppendingString:value];
//close tag
if (close)
xmlRow = [xmlRow stringByAppendingString:[self closeElement:element]];
return xmlRow;
}
【问题讨论】:
-
stackoverflow.com/questions/510269/… 使用它对你有多大用处
-
这对我来说也应该可以正常工作,但事实并非如此。只有当我在没有线程的情况下同步运行它时才会这样做。
标签: iphone objective-c nsstring nsthread