如何使用NSString的writeToFile方法

时间:2017-02-03 11:33:09

标签: objective-c

我正在尝试编写一个将所有https://repo1.maven.org/maven2/info/novatec/testit/livingdoc-confluence5-plugin/1.1.2/livingdoc-confluence5-plugin-1.1.2.jar写入文件的Objective-C程序。到目前为止,我已经尝试过:

#import <Foundation/Foundation.h>

int main(void) {
    [[NSFileManager defaultManager] createFileAtPath:@"knownTimeZoneNames.txt" contents:nil attributes:nil];


    for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
        [name writeToFile:"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
//      NSLog(@"The time zone is %@", name);

    }
    return 0;
}

请注意,如果我在NSLog的行中注释并注释掉writeToFile的行,则程序会打印预期的输出。但是,实际上,我收到有关不兼容指针类型的警告:

write_known_timezone_names.m: In function ‘main’:
write_known_timezone_names.m:10:3: warning: passing argument 1 of ‘writeToFile:atomically:encoding:error:’ from incompatible pointer type [-Wincompatible-pointer-types]
   [name writeToFile:"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
   ^
write_known_timezone_names.m:10:3: note: expected ‘struct NSString *’ but argument is of type ‘char *’
write_known_timezone_names.m:10:3: warning: passing argument 4 of ‘writeToFile:atomically:encoding:error:’ from incompatible pointer type [-Wincompatible-pointer-types]
write_known_timezone_names.m:10:3: note: expected ‘struct NSError **’ but argument is of type ‘id’
The file write_known_timezone_names.m has been compiled and can be run using the command ./write_known_timezone_names.

如果我尝试运行该程序,我会得到一个Segmentation fault

2017-02-03 12:31:49.485 write_known_timezone_names[23626] autorelease called without pool for object (0x17d6f90) of class NSDataStatic in thread <NSThread: 0x175e330>
2017-02-03 12:31:49.487 write_known_timezone_names[23626] autorelease called without pool for object (0x18e4f70) of class NSMutableDataMalloc in thread <NSThread: 0x175e330>
2017-02-03 12:31:49.489 write_known_timezone_names[23626] autorelease called without pool for object (0x18ec110) of class NSDataMalloc in thread <NSThread: 0x175e330>
Segmentation fault (core dumped)

如何修复此程序?

4 个答案:

答案 0 :(得分:2)

你在文件名之前错过了@。 正确的代码:

int main(void) {
 [[NSFileManager defaultManager] createFileAtPath:@"knownTimeZoneNames.txt" contents:nil attributes:nil];


for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
    [name writeToFile:@"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //      NSLog(@"The time zone is %@", name);

}
return 0;}

答案 1 :(得分:2)

  1. 关于Segmentation fault:由于错误消息自动释放而不使用池显示您必须将代码包装在自动释放池中

    @autoreleasepool {
    
        [[NSFileManager defaultManager] createFileAtPath:@"knownTimeZoneNames.txt" contents:nil attributes:nil];
    
        for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
            [name writeToFile:@"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
            //NSLog(@"The time zone is %@", name);
        }
        return 0;
    }
    
  2. 我不知道你要写文件的位置,但路径必须以斜杠开头,而在iOS中你必须以编程方式获得你有权写入的目录之一。 < / LI>
  3. 即使其他问题得到解决,您生成的文本文件也只包含一个行(最后一次迭代的名称)。要获得所有名称 - 每行一个 - 使用它而不是重复循环

    NSArray *nameArray = [NSTimeZone knownTimeZoneNames];
    NSString *names = [nameArray componentsJoinedByString:@"\n"];
    

答案 2 :(得分:1)

请试试这个

for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
     // You was trying to pass chars, but you have to pass string.   
    [name writeToFile:@"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //      NSLog(@"The time zone is %@", name);
}

答案 3 :(得分:1)

int main(void) {
 [[NSFileManager defaultManager] createFileAtPath:@"knownTimeZoneNames.txt" contents:nil attributes:nil];

NSMutableString *timeZones = [[NSMutableString alloc] init];
for (NSString *name in [NSTimeZone knownTimeZoneNames]) {
    [timeZones appendFormat:@"%@\n", name];    
}
[timeZones writeToFile:@"knownTimeZoneNames.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

return 0;
}