在搜索 属性 列表时如何检测错误?
How can I detect an error when searching through a property list?
在我的应用程序的第一个屏幕上,您输入了一个 4 位数的代码。当您按 "Done" 时,它会自动保存代码,应用程序会将视图切换到下一个屏幕。在新屏幕上,它会提取保存的代码并搜索 plist 以尝试找到与其关联的字符串。它目前工作完美,除非用户输入不在 plist 中的代码。
我如何教它发出代码不在 plist 中的错误警报?
这是第一个视图:
NSString *pureString = [[detectionString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:pureString forKey:@"beaverID"];
[defaults synchronize];
ViewController *Flip = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Flip animated:YES];
那么在第二个视图中:
- (void)viewDidLoad {
//Loading the unique student's ID code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:@"beaverID"];
//Loading the user's first name from the first .plist:
NSString *error2 = nil;
NSPropertyListFormat format2;
NSString *plistPath2;
NSString *rootPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file2 = @"User Data.plist";
plistPath2 = [rootPath2 stringByAppendingPathComponent:file2];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath2]) {
plistPath2 = [[NSBundle mainBundle] pathForResource:@"User Data" ofType:@"plist"];
}
NSData *plistData2 = [[NSFileManager defaultManager] contentsAtPath:plistPath2];
NSDictionary *tempDict2 = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistData2 mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format2 errorDescription:&error2];
NSString *person = [@"BCDS" stringByAppendingString:loadstring];
NSString *string = [tempDict2 objectForKey:person];
NSString *message = [@"Hello, " stringByAppendingString:string];
welcome.text = message;
}
只需检查 objectForKey
returns 是否为零:
NSString *string = [tempDict2 objectForKey:person];
if (string == nil)
[[[UIAlertView alloc]
initWithTitle:@"Error" message:@"Cannot find item"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
在我的应用程序的第一个屏幕上,您输入了一个 4 位数的代码。当您按 "Done" 时,它会自动保存代码,应用程序会将视图切换到下一个屏幕。在新屏幕上,它会提取保存的代码并搜索 plist 以尝试找到与其关联的字符串。它目前工作完美,除非用户输入不在 plist 中的代码。
我如何教它发出代码不在 plist 中的错误警报?
这是第一个视图:
NSString *pureString = [[detectionString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:pureString forKey:@"beaverID"];
[defaults synchronize];
ViewController *Flip = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Flip animated:YES];
那么在第二个视图中:
- (void)viewDidLoad {
//Loading the unique student's ID code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:@"beaverID"];
//Loading the user's first name from the first .plist:
NSString *error2 = nil;
NSPropertyListFormat format2;
NSString *plistPath2;
NSString *rootPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file2 = @"User Data.plist";
plistPath2 = [rootPath2 stringByAppendingPathComponent:file2];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath2]) {
plistPath2 = [[NSBundle mainBundle] pathForResource:@"User Data" ofType:@"plist"];
}
NSData *plistData2 = [[NSFileManager defaultManager] contentsAtPath:plistPath2];
NSDictionary *tempDict2 = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistData2 mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format2 errorDescription:&error2];
NSString *person = [@"BCDS" stringByAppendingString:loadstring];
NSString *string = [tempDict2 objectForKey:person];
NSString *message = [@"Hello, " stringByAppendingString:string];
welcome.text = message;
}
只需检查 objectForKey
returns 是否为零:
NSString *string = [tempDict2 objectForKey:person];
if (string == nil)
[[[UIAlertView alloc]
initWithTitle:@"Error" message:@"Cannot find item"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];