0

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?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
FoFa Ok
  • 3
  • 4
  • Where are you storing `1.txt`? – Mike D Mar 25 '13 at 20:21
  • 1
    This has been answered already, but you do not need to use `FILE *file` in Objective-C: http://stackoverflow.com/questions/1492335/read-text-file-programmatically-using-objective-c – Mike D Mar 25 '13 at 20:23
  • In the folder of the project which contain the header file , main file etc – FoFa Ok Mar 25 '13 at 20:26
  • Please see the link I posted. If that does not fully answer your question, please update with specifically what does not work. – Mike D Mar 25 '13 at 20:28
  • That little method contains 2 errors: `a = ` is an assignment, try something like `[a isEqualToString ...` and then you forgot to set `found` to true. – ott-- Mar 25 '13 at 20:56

1 Answers1

1

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.

enter image description here

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • @FoFa See also https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048-CJBGDEGD – Lorenzo B Mar 25 '13 at 21:32