Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check items in a plist file - cocoa?
    primarykey
    data
    text
    <p>I've looked at a bunch of other questions and they're not exactly what I need. I'm trying to set a NSTextField to a certain string based on the name of the file. I want to create a plist file with all the possible names of the file and their keys to set the NSTextField's string as. But I don't know how to have it check the plist for the items and if it's not there I want it to execute the code to leave the text field blank. Before I had if statements saying that if the filename equals this then make it say this but I couldn't get the else part to work and only one if worked. So I thought I'd have it check the plist but I don't know how to do that and use it with an if-else statement. This is what I had before:</p> <pre><code>NSString *inputName = [input stringValue]; NSString *theFileName = [[inputName lastPathComponent] stringByDeletingPathExtension]; if ([theFileName isEqualToString:@"038-3698-001"]) { [key setStringValue:@"7ed37d8c051da8f8d31b0ccf0980fa5ffa54770c7e68ecb5ebf28abe683cadf21a4a99ed"]; } if ([theFileName isEqualToString:@"038-3763-001"]) { [key setStringValue:@"a31ffd506c6711c5a0c52c9f0a2f7208a2f63ad9dd40506e70d80ea20a981eb1312bc774"]; } else { [key setStringValue:@""]; } </code></pre> <p>EDIT: I have it checking the plist for the key but now I need it to get the value for the key from the plist. Something like this:</p> <pre><code>NSString *inputName = [input stringValue]; NSString *theFileName = [[inputName lastPathComponent] stringByDeletingPathExtension]; NSDictionary *fileKeys = [[NSMutableDictionary alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Keys" ofType:@"plist"]]; if ([fileKeys valueForKey:theFileName]) { NSString *key = [NSString valueForKey:theFileName]; [filesField setStringValue:key]; } else { [filesField setStringValue:@""]; } </code></pre> <p>Nevermind I got it. This is correct code: </p> <pre><code>NSString *inputName = [input stringValue]; NSString *theFileName = [[inputName lastPathComponent] stringByDeletingPathExtension]; NSDictionary *fileKeys = [[NSMutableDictionary alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Keys" ofType:@"plist"]]; if ([fileKeys valueForKey:theFileName]) { NSString *key = [fileKeys valueForKey:theFileName]; [filesField setStringValue:key]; } else { [filesField setStringValue:@""]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload