Domanda

I have an view in an app that is a help feature.

This view consists of textview and a tableview. The table view consists of a load of questions, when user selects a question it displays the answer in the UITextView.

What I'm trying to do is hardcode this data. Now i can get views to work nice and easy just code behind isn't the cleanest and with the questions and answers always changing, I want this data easily stored. What I'm thinking is a NSMutableArray something like this.

NSMutableArray *HelpPage = [NSMutableArray arrayWithObjects: <answers,questions here>

how do I put say something like this in

NSMutableArray* HelpPage = [NSMutableArray arrayWithObjects: 
@"How do i close", @"Press close button"
@"How do i open", @"Press the open button",

Btw I should add these are only examples lol.

ideally I'd like it in its separate .h/.m file so people can just send me updated versions and I don't have to spend ages merging stuff.

Is NSMutableArray best way of doing this?

in other areas of the app, i record users ethnicity, what i did here was

.h file
+ (NSArray*) ArrayofEthnicity;

.m file
+ (NSArray*) ArrayofEthnicity{
    return [NSArray arraywithObjects: @"English", @"French", @"German", nil];
} 

and each time i need it calling

NSArray* Data = [EthnicityData ArrayofEthnicity];

is there a better way of doing this?

Thanks

È stato utile?

Soluzione

You can use NSMutableDictionary to store question and answer

NSMutableDictionary *qAndA = [[NSMutableDictionary alloc] init];
// add all Q&A with object is answer, key is question
[qAndA setObject:@"Press close button" forKey:@"How do i close"];
[qAndA setObject:@"Press the open button" forKey:@"How do i open"];
// to list all question
NSArray *questions = [qAndA allKeys];
.................................. 
// when need answer to a question
NSString *question = @"How do i close";
NSString *answer = [qAndA objectForKey:question];

Altri suggerimenti

The best way would be a property list file. (otherwise known as a .plist file)

Check the Apple Programming Guide for this... https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html

A quick example from the guide below. Essentially it's a good place to store parameters for your app.

Listing 1-2 Reading in and converting the XML property list

- (id) init {

    self = [super init];
    if (self) {
        NSString *errorDesc = nil;
        NSPropertyListFormat format;
        NSString *plistPath;
        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
           NSUserDomainMask, YES) objectAtIndex:0];
        plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
        }
        NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
        NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
            propertyListFromData:plistXML
            mutabilityOption:NSPropertyListMutableContainersAndLeaves
            format:&format
            errorDescription:&errorDesc];
        if (!temp) {
            NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        }
        self.personName = [temp objectForKey:@"Name"];
        self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];

    }
    return self;
}

It's depend how many time you have to use the questions and answers data and how many data you have to store.

If you have a lot of data to read a few times, you should write them into a .plist file.

If you have few questions and answer and you have to access them a lot of time in your app, then you can store them into an NSMutableArray or NSMutableDictionary stored in an singleton instance to be able to access it from anywhere in the code.

An other solution is to use Core Data. It's more long to implement but it's the strongest way.

Core Data provide you to use an SQLite data base inside your app to store and read your data, but you can also use it to read your data from a remote web service. One time again, this solution is the longest to implement but if you need a very optimized and very well designed solution, this is the best way.

json is the most readable format AFAIK. Your JSON file will look like this

{
     "How do I close": "Press close",
     "How do I open": "Press open"
}

Or, in array format,

[
     "How do I close", "Press close",
     "How do I open", "Press open"
]

Loading would be a few lines of code with NSJSONSerialization.

Good luck!

In my opinion plist is not the best way to store human readable text and JSON is not the most readable format. Especially if the maintainer of the files aren't developers, as I guess it's the case.

I'd rather write the help texts in markdown, with some fixed simple structure, like header + free text for each question. Or more elaborated with lists, links, whatever you need.

# Question 1

Answer 1 goes here.

# Question 2

Answer 2...

Then I'll write a parser to import the text into an internal format in order to show them to the user (an array of dictionaries or something similar).

You write this once, then just update the text files. The maintainer can use a markdown editor to validate the syntax.

Fortunately there are a ton of markdown parsers to hack this. I recommend libsoldout, designed specifically to write custom parsers.

I wrote a sample project to demo it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top