【问题标题】:Method returns NSString as nil方法将 NSString 返回为 nil
【发布时间】: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


【解决方案1】:

我怀疑你没有为线程创建自动释放池。每个线程都必须有自己的自动释放池。

【讨论】:

  • 我确实有自动释放池。可能自动释放会解决问题。但是我尝试分配字符串但得到了相同的效果。
  • 您的 XML 对线程调用有效吗?您的新编辑与您之前的简单示例几乎没有相似之处,这让我想到了自动释放池。
  • 是的。我没有发布所有 createSelectPhonesRequest 因为没有必要。里面的所有行都是一样的。你注意到我所有的方法都是静态的吗?
【解决方案2】:

我认为您应该正确初始化字符串,例如,

NSString *myString = [NSString stringWithString:@"Hello"];

【讨论】:

  • 它没有帮助。我仍然在调用方法中得到零。
  • NSString *myString = [[NSString alloc] initWithString:@"Hello"]; //然后你的代码返回[myString autoRelease];
  • 得到了同样的效果。也许是因为我的方法是静态的,所以有些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 2013-09-07
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多