Skip to content

Instantly share code, notes, and snippets.

@kuetsuhara kuetsuhara/gist:8111076
Last active Jan 1, 2016

Embed
What would you like to do?
CBLModelでCreate
// 新規ドキュメントの作成
- (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
You can’t perform that action at this time.