将NSString类型写入文件 [英] writing NSString type to a file

查看:221
本文介绍了将NSString类型写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在 appdelegate

上实现以下函数,但我需要写 NSString 类型而不是典型的浮点值。

因为xcode不允许对象在我想要的位置,

我使用 char * 而不是如下,其中要传递的数据类型为 NSString

,它不工作...

我如何操作它,以便我可以写 NSString 数据类型?

我应该进行一些转换吗?

请帮助我。


I'm implementing the following function on appdelegate,
but I need to write NSString type instead of typical float values.
Since xcode doesn't allow an object to be in my desired position,
I used char* instead as follows, where as my data to be passed are of type NSString.
As expected, it doesn't work...
How could I manipulate it so that I could write NSString data type?
Should I make some conversion?
Please help me out..

- (void)addHallOfFamer:(char*)newHofer{
    [hofArray addObject:[NSString stringWithFormat:@"%@",newHofer]];
    [hofArray sortUsingSelector:@selector(compare:)];
    NSArray* paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* hofArrayPath = [documentsDirectory
                          stringByAppendingPathComponent:@"hofers.plist"];
    [hofArray writeToFile:hofArrayPath atomically:YES];
}

(已添加)
下面是我如何调用NSStrings从另一个视图,这不反映我的更新。

(added) following is how I'm calling the written NSStrings from another view, which doesn't reflect my updating.

MainAppDelegate* delegate;
delegate = (MainAppDelegate*)[[UIApplication sharedApplication] delegate];
NSArray *hofers = [[delegate.hofArray reverseObjectEnumerator] allObjects];
hoferName1.text = [NSString stringWithFormat:@"%@",[hofers objectAtIndex:0]];


推荐答案

char * 参数,您需要使用%s作为格式指令,而不是%@。

First, with the current char * argument, you need to use %s as your format directive, not %@.

其次,使用 NSString * 作为参数,只需将它添加到hofArray。

Second, to use an NSString * as your argument, just add it to hofArray.

这篇关于将NSString类型写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆