ios - Automatically Generated Primary Key -
i developing ios app.using core data storing data.but primary keys automatically generated.data can stored id.
//code nsmanagedobjectcontext *context = [self managedobjectcontext]; nsmanagedobject *userobj = [nsentitydescription insertnewobjectforentityforname:@"detailsave" inmanagedobjectcontext:context]; [userobj setvalue:[nsnumber numberwithinteger:_userobj.userid] forkey:@"id"]; [userobj setvalue:_titlelabel.text forkey:@"title"]; nsinteger valint = 1; nsnumber *val = [nsnumber numberwithinteger:valint]; [userobj setvalue:val forkey:@"quantity"]; nsstring *maxpurch = [dict objectforkey:@"_max_purchases_per_user"]; nsnumber *maxval = [nsnumber numberwithinteger:[maxpurch integervalue]]; [userobj setvalue:maxval forkey:@"max_purch"]; nsstring *maxpurch = [dict objectforkey:@"_max_purchases"]; nsnumber *max = [nsnumber numberwithinteger:[maxpurch integervalue]]; [userobj setvalue:max forkey:@"available"]; nsstring *baseprice = [dict objectforkey:@"_base_price"]; nsnumber *baseprice = [nsnumber numberwithinteger:[baseprice integervalue]]; [userobj setvalue:baseprice forkey:@"amount"]; //nslog(@"%@",userobj); nslog(@"app dir: %@",[[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject]); nserror *error = nil; if (![context save:&error]) { nslog(@"can't save! %@ %@", error, [error localizeddescription]); } nsfetchrequest *request = [[nsfetchrequest alloc] init]; // count entity [request setentity:[nsentitydescription entityforname:@"detailsave" inmanagedobjectcontext:context]]; [request setincludessubentities:no]; nserror *err; nsuinteger count = [context countforfetchrequest:request error:&err]; if(count == nsnotfound) { }
if m=8, or if m close 8 (e.g. 9 or 10), takes while , can more clever.
-(nsmutablearray *)geteightrandomlessthan:(int)m { nsmutablearray *listofnumbers = [[nsmutablearray alloc] init]; (int i=0 ; i<m ; ++i) { [listofnumbers addobject:[nsnumber numberwithint:i]]; // add 1 numbers between 1 , m rather 0 , m-1 } nsmutablearray *uniquenumbers = [[[nsmutablearray alloc] init] autorelease]; int r; while ([uniquenumbers count] < 8) { r = arc4random() % [listofnumbers count]; if (![uniquenumbers containsobject:[listofnumbers objectatindex:r]]) { [uniquenumbers addobject:[listofnumbers objectatindex:r]]; } } [listofnumbers release]; return uniquenumbers; }
or
unsigned int permuteqpr(unsigned int x) { static const unsigned int prime = 4294967291; if (x >= prime) return x; // 5 integers out of range mapped themselves. unsigned int residue = ((unsigned long long) x * x) % prime; return (x <= prime / 2) ? residue : prime - residue; }
Comments
Post a Comment