Last active
January 1, 2016 07:19
-
-
Save kuetsuhara/8111076 to your computer and use it in GitHub Desktop.
CBLModelでCreate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 新規ドキュメントの作成 | |
- (void)createDocument{ | |
// Appdelegateを呼ぶ | |
AppDelegate *ap = ApplicationDelegate; | |
// カウントの読み込み | |
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; | |
// カウントが0だったら(ユーザデフォルトが存在しなければ)1にする | |
int num = [userDefault integerForKey:NUMBER_KEY]; | |
if (num == 0) { | |
num = 1; | |
} | |
// カウント文字列の作成 | |
NSString *numberStr = [NSString stringWithFormat:@"%d",num]; | |
// カラのmodelを作成 | |
ShoppingItem *item = [[ShoppingItem alloc] initWithNewDocumentInDatabase:ap.database]; | |
// プロパティに各種値を入れる | |
item.type = @"spItem"; | |
item.text = numberStr; | |
item.check = NO; | |
item.created_at = [NSDate date]; | |
item.dictionary = @{@"key": @"value", | |
@"key2":@"value2"}; | |
item.array = @[@"obj1",@"obj2",@"obj3",@"obj4",@"obj5"]; | |
// Save | |
NSError *error; | |
BOOL ok = [item save:&error]; | |
// Saveが成功したらインクリメント | |
if (ok) { | |
++num; | |
// ユーザデフォルトのに保存 | |
[userDefault setInteger:num forKey:NUMBER_KEY]; | |
[userDefault synchronize]; | |
} | |
else{ | |
[self showErrorAlert: @"Couldn't read documents"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment