2013-03-25 34 views
0

我試圖開發一個iOS應用程序,允許用戶輸入一個單詞,然後按下按鈕,得到了這個詞的定義從Objective-C中的文件讀取數據,如何?

其實我有一個包含單詞及其定義,使得

文件
Apple , the definition 
orange , the definition 

我寫的代碼,但我不知道爲什麼它不工作

#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 

誰能幫我弄清楚什麼是錯的?

+0

你在哪裏存儲'1.txt'? – 2013-03-25 20:21:40

+1

這已經被回答了,但是你不需要在Objective-C中使用'FILE * file':http://stackoverflow.com/questions/1492335/read-text-file-programmatically-using-objective-c – 2013-03-25 20:23:15

+0

在包含頭文件,主文件等的項目文件夾中 – 2013-03-25 20:26:07

回答

1

恕我直言,一個簡單的方法來實現這一點是通過plist。

在這裏,一個小例子來實現你想要的。

1)創建你的plist文件。

在您的項目中,轉到「添加新文件」。在iOS左側的列中(例如),選擇「資源」。在主面板中選擇一個「Property List」文件。保存一個像「Defs」這樣的名字。

你的plist應該看起來像下面這樣。

enter image description here

2)閱讀plist文件(註釋代碼)

- (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]); 
    } 
} 

的一些注意事項

以上關於如何使用的plist一個簡單的例子。它沒有提供一個完整的例子來實現你想要的。基於此,你將能夠實現你的目標。

您應該需要一個屬性來引用您檢索的字典。所以,例如,在你的.m。

@interface ViewController() 

@property (nonatomic, strong) NSDictionary* definitions; 

@end 

@implementation ViewController 

// other code here 

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

使用definitions檢索您感興趣的定義。

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

希望有所幫助。

+0

@FoFa另見https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048-CJBGDEGD – 2013-03-25 21:32:57