gpt4 book ai didi

objective-c - 输入字符串在静态方法中设置后为空,为什么?

转载 作者:行者123 更新时间:2023-12-03 17:13:57 25 4
gpt4 key购买 nike

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

我有两种方法:

+(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”的底部方法会检索空的 plistPath。为什么以及如何解决这个问题?

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

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

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

最佳答案

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

您正在底部方法中创建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]];

因此,无论您在顶部方法中的 plistPath 中设置什么,都不会传递回底部方法,请认为顶部 plistPath 在该方法执行以下操作时被释放返回。因此底部方法中的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;
}

关于objective-c - 输入字符串在静态方法中设置后为空,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12688918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com