在静态方法中设置后,Inputstring为null,为什么?

时间:2012-10-02 10:50:43

标签: objective-c cocoa nsstring static-methods

我不确定在我的特定情况下如何管理内存......

我有两种方法:

+(NSMutableDictionary *)loadPlist: (NSString*) name
                     andErrorDesc: (NSString*) errorDesc
                        andFormat: (NSPropertyListFormat*) format
                     andplistPath: (NSMutableString*) plistPath
{    
    NSString * destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    destPath = [destPath stringByAppendingPathComponent:
                 [NSString stringWithFormat:@"%@.plist", name]];

    if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
    {
        [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:name ofType:@"plist"] toPath:destPath error:nil];
    }

    plistPath = [NSMutableString stringWithString:[destPath copy]];

    NSData * plistXML =
    [[NSFileManager defaultManager] contentsAtPath:plistPath];

    NSLog(@"AFTER plistPath: \n%@",plistPath);


    return
    (NSMutableDictionary *)[NSPropertyListSerialization
                            propertyListFromData:plistXML
                            mutabilityOption:NSPropertyListMutableContainersAndLeaves
                            format:format
                            errorDescription:&errorDesc];
}


+(bool)writeToCache:(NSString*) data andField: (NSString*) field
{
    NSString * errorDesc = nil;
    NSPropertyListFormat format;
    NSMutableString * plistPath;
    NSMutableDictionary * temp = [BPUtils loadPlist:@"cache" andErrorDesc:errorDesc andFormat:&format andplistPath:plistPath];

    if (!temp)
    {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return false;
    }

    NSMutableArray * arr = [temp objectForKey:field];
    [arr addObject:data];

    NSLog(@"path: %@",plistPath);

    // Write to plist
    bool res = [temp writeToFile:plistPath atomically:YES];

    NSLog(@"RES: %d", res);

    return true;
}

问题是,在上面的方法设置之后,将“plistPath”发送到上述方法的bottom方法将重新检测空plistPath。为什么以及如何解决这个问题?

NSLog(@"path: %@",plistPath); 
底部方法中的

显示为null,为什么?

我使用ARC。此外,“destPath”已设置并显示正确的路径。

1 个答案:

答案 0 :(得分:1)

我相信你可能会有点困惑。

您正在底部方法中创建plistPath。然后你将plistPath传递给

     [BPUtils loadPlist:@"cache" andErrorDesc:errorDesc andFormat:&format andplistPath:plistPath];

plistPathNULL

     NSMutableString * plistPath; // Is NULL

但是一旦它被传递到本地plistPath接管。

+(NSMutableDictionary *)loadPlist: (NSString*) name
                 andErrorDesc: (NSString*) errorDesc
                    andFormat: (NSPropertyListFormat*) format
                 andplistPath: (NSMutableString*) plistPath // Notice the local plistPath variable. This is the one you are playing with in this method.

此时您正在设置plistPath但请记住它仍然只是一个局部变量而不是实例变量。因此按钮方法永远不会知道它被设置,就按钮方法而言它仍然是NULL

plistPath = [NSMutableString stringWithString:[destPath copy]];

因此,无论您在top方法中plistPath中设置的内容都不会传递回底部方法,请在方法执行plistPath时将顶部return视为已释放。 因此,底部方法中的plistPath将保持NULL

请尝试使用解决方案

 static NSMutableString *yourNewStringforPlistPath; //This will be NULL

 +(NSMutableDictionary *)loadPlist: (NSString*) name
                 andErrorDesc: (NSString*) errorDesc
                    andFormat: (NSPropertyListFormat*) format
                 andplistPath: (NSMutableString*) plistPath
 {    
 NSString * destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

 destPath = [destPath stringByAppendingPathComponent:
             [NSString stringWithFormat:@"%@.plist", name]];

 if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
 {
     [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:name ofType:@"plist"] toPath:destPath error:nil];
 }

 yourNewStringforPlistPath = [NSMutableString stringWithString:[destPath copy]];

 NSData * plistXML =
 [[NSFileManager defaultManager] contentsAtPath:yourNewStringforPlistPath];

 NSLog(@"AFTER plistPath: \n%@",yourNewStringforPlistPath);


 return
 (NSMutableDictionary *)[NSPropertyListSerialization
                        propertyListFromData:plistXML
                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                        format:format
                        errorDescription:&errorDesc];
  }


  +(bool)writeToCache:(NSString*) data andField: (NSString*) field
  {
NSString * errorDesc = nil;
NSPropertyListFormat format;
NSMutableDictionary * temp = [BPUtils loadPlist:@"cache" andErrorDesc:errorDesc andFormat:&format andplistPath:[NSNull null]]; // As this is already NULL you don't really need to pass yourNewStringforPlistPath in unless in the future this value can be set before this.

 if (!temp)
 {
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    return false;
 }

  NSMutableArray * arr = [temp objectForKey:field];
 [arr addObject:data];

 NSLog(@"path: %@",yourNewStringforPlistPath);

 // Write to plist
 bool res = [temp writeToFile:yourNewStringforPlistPath atomically:YES];

 NSLog(@"RES: %d", res);

 return true;
 }