このチュートリアルでは、各種データをNSUserDefaultsを使って、保存、抽出する方法について説明します。
NSUserDefaults は、データベースの知識を必要としないので、SQLite3のようなデータベースを必要としない(ゲームのハイスコア、ログイン情報、アプリの設定など)、小規模のデータを扱う場合には便利で簡単です。
今回は、例として、初回時にアプリを起動し、名前を入力すると、保存され、次回以降の起動時には、保存された名前が自動的に名前が表示されるアプリケーションを作成します。
…
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @”Cell”; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //ref of Addressbook ABAddressBookRef book = ABAddressBookCreate(); //Count of Addressbook //CFIndex cnt = ABAddressBookGetPersonCount(book); //NSLog(@”addressbook count is %d “,cnt); //AllRecords of Addressbook CFArrayRef records = ABAddressBookCopyArrayOfAllPeople(book); //record at index=i ABRecordRef person = CFArrayGetValueAtIndex(records,indexPath.row); NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); if (firstName == nil) { firstName = @””; } if (lastName == nil) { lastName = @””; } cell.textLabel.text = [NSString stringWithFormat:@”%@ %@”,lastName,firstName]; return cell; }
graffiti on the web
この投稿へのコメント
コメントはまだありません。