//
// ViewController.m
// Crystal Bal
//
// Created by Vaio on 14/11/2.
// Copyright (c) 2014年 Vaio. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (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)buttonPressed:(UIButton *)sender {
NSLog(@"buttonPressed"); //后台打印
//字符串学习
NSString *myString=@"my name is Lucy";
NSString *myStringUppercase=[myString uppercaseString];
NSLog(@"myString =%@",myString);
NSLog(@"myStringUppercase =%@",myStringUppercase);
//数组学习
NSArray *predictionArray = [NSArray alloc];
//数组中有9个元素,索引范围是0-8 数字9超过数组索引范围
predictionArray = [predictionArray initWithObjects:@"it is certain",
@"it is decidely so",
@"3333333",
@"444444",
@"55555555",
@"6666666666",
@"777777777777",
@"88888888888",
@"9999999999",nil];
NSString *predictonArrayOne = [predictionArray objectAtIndex:0];
NSString *predictonArrayFive = [predictionArray objectAtIndex:4];
NSLog(@"predictonArrayOne = %@",predictonArrayOne);
NSLog(@"predictionArrayFive =%@",predictonArrayFive);
NSString *predictonArrayTen = [predictionArray objectAtIndex:9];
NSLog(@"predictonArrayTen = %@",predictonArrayTen);
}
@end
此时报错
2014-11-15 21:17:06.353 Crystal Bal[709:27886] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 9 beyond bounds [0 .. 8]'
解决方法为在数组中加入第十行或取消输出predictonArrayTen