开发者

How can a Hashtable access data from a text file in Objective-C?

I have already created a project to read a file and also to displ开发者_Python百科ay it. Now i need to create a hashtable and access the data from the created file( text file ) in Objective-C for iPhone-OS. Pls do give suggestion...


Your question does not make sense.

A hash-table (I think you're referring to what we call an NSDictionary, or a set of key-value pairs) is a data structure.

A file is a series of bytes that can be interpreted in a multitude of ways. It can be interpreted as audio, video, or textual data. It can be interpreted as something entirely different.

So you want to "create a hashtable". That's easy enough:

NSDictionary * myDictionary = [[NSDictionary alloc] init];

And you want to access the data from the created file:

NSString * fileContents = [[NSString alloc] initWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
//or
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathToFile];

What you do beyond that is unspecified in your question.

EDIT:

You want to know how to access the file contents "automatically". Well, using the NSString method above, you can parse it yourself. Using the NSFileHandle method you can read in the bytes yourself. The only way to get a file to automatically convert into an NSDictionary is to use -[NSDictionary initWithContentsOfFile:]. However, in order to use that, you have to have previously used -[NSDictionary writeToFile:atomically:] to create the file.


NSMutableDictionary is a modifiable hash table implementation in Cocoa. -setObject:forKey: sets an entry in the dictionary, but parsing your text file into keys and values is up to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜