1

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

我有两种方法:

+(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”中发送到上述方法的底部方法在上述方法设置后检索了一个空 plistPath。为什么以及如何解决这个问题?

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

在底部方法中显示null,为什么?

我使用 ARC。还设置了“destPath”并显示了正确的路径。

4

1 回答 1

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]];

因此,无论您在plistPathtop 方法中设置什么,都不会传递回 bottom 方法,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;
 }
于 2012-10-02T11:10:12.680 回答