How can i check the array has object or not [closed] - iphone

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i want to check if the old array has object or not if the old array has the object it should show me the button if the oldArray has zero object the button should be hidden the code is given below thanks...
-(void)viewWillAppear:(BOOL)animated
{
GET_DEFAULTS
NSMutableArray *array = [defaults objectForKey:kShouldResume];
NSData *dataRepresentingSavedArray = [defaults objectForKey:kShouldResume];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
array = [[NSMutableArray alloc] initWithArray:oldSavedArray];
if ([oldSavedArray containsObject])
{
btnResumeGame.hidden=NO;
}
else
{
btnResumeGame.hidden=YES;
}
}
else
{
array = [[NSMutableArray alloc] init];
}
}
}

Array has property count.
You can check weather count is zero or more than that as you require..
like
oldSavedArray.count

use this code:
if ( [oldSavedArray count]>0 ){
btnResumeGame.hidden=NO;
}
else{
btnResumeGame.hidden=YES;
}

Related

How to check whether all elements in an array are null or not [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a code in which i want to do some thing when all elements in an array are null. Please help me in doing this.
This is simple. You have to check your array by using this line
int count = 0;
for (int i=0; i<array.count; i++)
{
NSString *str = [array objectAtIndex:i];
if (str == (id)[NSNull null] && str.length == 0 && [str isEqualToString:#"<null>"])
{
count++;
if (count == array.count)
{
// here you get all object are null
}
}
}
Good Luck !!!
NSArray and other collections can't take nil as a value, since nil is the "sentinel value" for when the collection ends. You can find if an object is null by using:
if (myObject == [NSNull null])
{
// do something because the object is null
}
Or
If you want to check if it is empty:
if ([myMutableArray count] == 0) { ... }
If you want to check if the variable is nil:
if (!myMutableArray) { ... }
or:
if (myMutableArray == nil) { ... }
if (!yourarray || !yourarray.count){
}
If statement to check if array is not null and if not checks if it is empty... Then whatever you like...
If the 'array' in question is an NSArray or subtype, then you have nothing to do because the collection forbids nil elements.
Otherwise, maybe you are interested in enumerating the values against the NSNull instance:
bool Foo(NSArray * pArray) {
assert(pArray.count);
id nul = NSNull.null;
for (id at in pArray) {
if (at != nul)
return false;
}
return true;
}
use this function
-(BOOL)checkArrayHavingNullEntry:(NSMutableArray *)array
{
for (id obj in array) {
if ([obj isKindOfClass:[NSNull class]])
return false;
}
return true;
}

Anniversary from contacts in iPhone [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm able to get birth dates from contacts but not the anniversary dates on iPhone. I have used KABPersonAnniversaryLabel but it is giving an error.
This code will fetch anniversary for all contacts,
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef anniversaries = ABRecordCopyValue(person, kABPersonDateProperty);
NSString *anniversaryLabel;
for (CFIndex j=0; j < ABMultiValueGetCount(anniversaries); j++) {
anniversaryLabel = (NSString*)ABMultiValueCopyLabelAtIndex(anniversaries, j);
if([anniversaryLabel isEqualToString:(NSString *)kABPersonAnniversaryLabel])
{
NSDate *anniversaryDate=(NSDate *)ABMultiValueCopyValueAtIndex(anniversaries, j);
NSLog(#"%#",anniversaryDate);
}
}
CFRelease(anniversaries);
}
CFRelease(addressBook);
CFRelease(people);

Get index of array object [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is the code I'm trying to finish
-(IBAction)theButtonIsSelected:(id)sender {
NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:#"Yes" forKey:#"Favorite"];
NSString *nameString = [mutDict valueForKey:#"Name"];
NSArray *allObjects;
allObjects = [[NSArray alloc] initWithContentsOfFile:path];
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjects];
int index;
//I think I just need a little piece right here to set the current allObjectsIndex to match nameString?
[tmpMutArr replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:mutDict]];
allObjects = nil;
allObjects = [[NSArray alloc] initWithArray:tmpMutArr];
[allObjects writeToFile:path atomically:YES];
}
This is my question:
if (what I'm trying to do above can be done) {
How to finish it?
} else {
How to make a function to change the "Favorite" key's value of plist object,
when detailsDataSource not always containing the complete list of objects?
That's why I'm trying to include allObjects and index in this code.
}
EDIT:
Code now look like this:
NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:#"Yes" forKey:#"Favorite"];
NSString *nameString = [[detailsDataSource objectAtIndex:detailIndex] valueForKey:#"Name"];
NSArray *allObjectsArray = [[NSArray alloc] initWithContentsOfFile:path];
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray];
if(int i=0;i<[tmpMutArr count];i++)
//Errors ^here and here^
{
if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i];
if([tempDict valueForKey:#"Name" == [NSString stringWithFormat:#"#%", nameString];) //Is this correct?
{
index = i; //index of dictionary
}
}
}
[tmpMutArr replaceObjectAtIndex:i withObject:[NSDictionary dictionaryWithDictionary:mutDict]];
allObjectsArray = nil;
allObjectsArray = [[NSArray alloc] initWithArray:tmpMutArr];
[allObjectsArray writeToFile:path atomically:YES];
Errors: 1 Expected expression 2 undeclared identifier 'i' how to declare I and fix the other error?
You can get index of dictionary like this:
if(int i=0;i<[tmpMutArr count];i++)
{
if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i];
if([tempDict objectForKey:#"Favorite")
{
index = i; // here u have your index of dictionary
}
}
}

Transforming NSMutableArray values [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've been looking for this, but can't find the answer.
I have an NSMutableArray with values who_1, what_2, where_3 etc.
I want to transform this into who, what, where etc.
I already have the value of the integer as a variable, and _ is just a string.
What steps should I take to have all these arrayvalues transformed?
NSArray * arrB = [[NSArray alloc]initWithObjects:#"apple_a",#"ball_b",#"cat_c",#"doll_d",nil];
NSMutableArray * arrA = [[NSMutableArray alloc]init];
for(NSString *strData in arrB)
{
NSArray *arr = [strData componentsSeparatedByString:#"_"];
[arrA addObject:[arr objectAtIndex:0]];
}
and this would be your output
arrA:(
apple,
ball,
cat,
doll
)
You need to apply logic for that, You cant find answers to tricky Questions :)
You need to run a loop.
Separate string with '_'
Loop
for(NSString *s in ary)
{
NSArray *a = [s componentsSeparatedByString:#"_"];
[anotherArray addObject:[a objectAtIndex:0]];
}
and update your array..
Following might help you -
NSRange range = [string rangeOfString:#"_"];
NSString *finalString = [originalString substringToIndex:range.location];
you can have this in loop.
Or you can go for componentSeperatedByStrings.
This might help you
NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"_0123456789"];
_string = [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#""];
[tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(#"%#", tmpAry); // Gives the modified array
[tmpAry release];

how to get last updated value in sqlite3 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hello Friends,
I have an application that will do the synchronization with the database to web-service. I have one table list in that I want to update, delete and insert the list. When the user click on the synchronization button I want to send the data which will be updated or changed in the database. The web-service are different for add, update or delete I want send only updated records.
For that what can I do for that any suggestion.
Thanks in advance,
Hardik Patel
You can maintain an extra column for timestamp.
So whenever a record is changed you should update the timestamp. (record_timestamp)
You have to maintain the time at which you have sent the data successfully to your server. (last_updated_timestamp)
So at the time of sync, you will send only those records to server whose
record_timestamp > last_updated_timestamp
The last_updated_timestamp will be updated every time successful sync is performed.
I hope this helps.
This link will help you compare time stamps in sqlite Timestamp comparison
You use sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
- (NSNumber *)executeSQL:(NSString *)sql withCallback:(void *)callbackFunction context:(id)contextObject {
NSString *path = [self pathToDB];
sqlite3 *db = NULL;
int rc = SQLITE_OK;
NSInteger lastRowId = 0;
rc = sqlite3_open([path UTF8String], &db);
if(SQLITE_OK != rc) {
NSLog(#"Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return nil;
}else {
char *zErrMsg = NULL;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);
if(SQLITE_OK != rc) {
NSLog(#"Can't run query '%#' error message: %s\n", sql, sqlite3_errmsg(db));
sqlite3_free(zErrMsg);
}
lastRowId = sqlite3_last_insert_rowid(db);
sqlite3_close(db);
[pool release];
}
NSNumber *lastInsertRowId = nil;
if(0 != lastRowId) {
lastInsertRowId = [NSNumber numberWithInteger:lastRowId];
}
return lastInsertRowId;
}