在Objective-C中从文件读取数据,如何? [英] Reading data from a file in Objective-C, how to?

查看:132
本文介绍了在Objective-C中从文件读取数据,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个iOS应用程序,允许用户输入一个单词,然后按下按钮来获得单词的定义。

其实我有包含单词及其定义的文件

  Apple,定义
orange,定义

我编写了代码,但是我不知道为什么它不工作

  #importViewController.h

NSString * readLineAsNSString(FILE * file);

@interface ViewController()

@end

@implementation ViewController
@synthesize text;
@synthesize lab;
- (void)viewDidLoad
{
[super viewDidLoad];
//在加载视图之后,通常从笔尖执行任何其他设置。
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理任何可以重新创建的资源。

$ b - (IBAction)butt:(id)sender {
FILE * file = fopen(1.txt,r);
NSString * a = text.text;
bool found = FALSE;

while(!feof(file)||!found)
{
NSString * line = readLineAsNSString(file);
if((a = [[line componentsSeparatedByString:@,] objectAtIndex:1])){
lab.text = [[line componentsSeparatedByString:@,] objectAtIndex:0];
}

fclose(file);
}
}

@end

有人帮我弄清楚有什么不对吗?解决方案

恕我直言,一个简单的方法来实现这个是通过plist。 >

这里有一个小例子来实现你想要的功能。

<1>创建你的plist文件。 strong>



在您的项目中,转到添加新文件。在左边的列中,在iOS(例如)下,选择资源。在主面板中选择一个属性列表文件。保存一个像Defs这样的名字。



你的plist应该看起来像下面这样:



2)阅读plist文件(代码中的注释)

   - (void)readDefinitionsFile 
{
/ /抓住plist所在的路径,这个plist附带了主应用程序包
NSString * plistPath = [[NSBundle mainBundle] pathForResource:@DefsofType:@plist];

//从您检索的plist开始创建一个字典
NSDictionary * definitions = [NSDictionary dictionaryWithContentsOfFile:plistPath];

//只是为了测试目的,读取与该字典相关的键和值
([定义allKeys]中的NSString *键){
NSLog(@definition \%@ \是\%@ \,键,[定义objectForKey:键]);




一些笔记 p>

以上是关于如何使用plist的简单示例。它没有提供一个完整的例子来实现你想要的。基于这一点,你将能够达到你的目标。

你应该需要一个属性来引用你检索的字典。所以,例如,你的.m。

pre $ $ $ c $ @interface ViewController
$ b $ @property(非原子,强大)NSDictionary *的定义;

@end

@implementation ViewController

//这里的其他代码

//在readDefinitionsFile方法中
self.definitions = [NSDictionary dictionaryWithContentsOfFile:plistPath];

使用定义来检索您的定义感兴趣。

  NSString * definition = [self.definitions objectForKey:@aKeyYouWillRetrieveFromSomewhere]; 
if(definition){
NSLog(@definition is%@ for key%@,definition,@aKeyYouWillRetrieveFromSomewhere);
}其他{
NSLog(@没有定义键%@,@aKeyYouWillRetrieveFromSomewhere);
}

希望有帮助。


I'm trying to develop an iOS app , that allow the user to enter a word then press the button to get the definition of the word

Actually I have a file which contains the words and their definitions such that

Apple , the definition 
orange , the definition 

I wrote the code but I don't know why it's not working

#import "ViewController.h"

NSString *readLineAsNSString(FILE *file);

@interface ViewController ()

@end

@implementation ViewController
@synthesize text;
@synthesize lab;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)butt:(id)sender {
    FILE *file = fopen("1.txt", "r");
    NSString *a = text.text ;
    bool found =FALSE;

    while(!feof(file)|| !found )
    {
      NSString *line = readLineAsNSString(file);
        if ((a= [[line componentsSeparatedByString:@","] objectAtIndex:1])) {
            lab.text = [[line componentsSeparatedByString:@","] objectAtIndex:0];
        }

    fclose(file);
    }
}

@end

Can anyone help me to figure out what is wrong?

解决方案

IMHO, a simple way to achieve this is through plist.

Here, a small example to achieve what you want.

1) Create your plist file.

In your project, go and "Add New File". In the left column, under iOS (for example), select "Resource". In the main panel select a "Property List" file. Save with a name like "Defs".

Your plist should look like the following.

2) Read the plist file (comments in the code)

- (void)readDefinitionsFile
{
    // grab the path where the plist is located, this plist ships with the main app bundle
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Defs" ofType:@"plist"];

    // create a dictionary starting from the plist you retrieved
    NSDictionary* definitions = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    // JUST FOR TEST PURPOSES, read the keys and the values associated with that dictionary
    for (NSString* key in [definitions allKeys]) {
        NSLog(@"definition for key \"%@\" is \"%@\"", key, [definitions objectForKey:key]);
    }
}

Some notes

Above a simple example on how to use plist. It does not provide a complete example to achieve what you want. Based on that you will be able to reach your goal.

You should need a property to reference the dictionary you retrieved. So, for example, in your .m.

@interface ViewController ()

@property (nonatomic, strong) NSDictionary* definitions;

@end

@implementation ViewController

// other code here

// within readDefinitionsFile method
self.definitions = [NSDictionary dictionaryWithContentsOfFile:plistPath];

Use definitions to retrieve the definition you are interested in.

NSString* definition = [self.definitions objectForKey:@"aKeyYouWillRetrieveFromSomewhere"];
if(definition) {
    NSLog(@"definition is %@ for key %@", definition, @"aKeyYouWillRetrieveFromSomewhere");
} else {
    NSLog(@"no definition for key %@", @"aKeyYouWillRetrieveFromSomewhere");
}

Hope that helps.

这篇关于在Objective-C中从文件读取数据,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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