Good day!
I have this code to remove the a particular database in a NSMutableArray.
-(void)removeDatabaseName:(NSString*)databaseName {
// remove database from array of databases
for (int index = 0; index < [[self.openDatabases objectForKey:#"databaseName"] count]; index++) {
if ([[self.openDatabases objectForKey:#"databaseName"] objectAtIndex:index] == databaseName) {
//Match
[[self.openDatabases objectForKey:#"database"] removeObjectAtIndex:index];
[[self.openDatabases objectForKey:#"databaseName"] removeObjectAtIndex:index];
[[self.openDatabases objectForKey:#"location"] removeObjectAtIndex:index];
NSLog(#"removed database");
break;
}
}
}
As a result, it displays the NSLog(#"removed database"); but I don't know if it was totally removed in the NSMutableArray or if I am wrong can I have at least remove the specific file?
Related
I am adding NSInteger object as NSNumber in a NSMutableArray. But now I am such in condition like that I need to check a NSInteger is in the NSMutableArray. If the array contains the value then I will execute my next code else I will execute other code. I want to execute else condition only if array doesn't contain the value. I tried this code:
for(int i=0; i<self.indexArray.count; i++){
if([[self.indexArray objectAtIndex:i] integerValue]==self.selectedIndex){
NSLog(#"execute if in the array");
}
else{
NSLog(#"execute if not in the array");
}
}
Though the array contains the value, else is executing for the loop. My question is how will I check a value which one is in a NSMutableArray or not.
This is the simple solution:
BOOL containsSelected = NO;
for (int i = 0; i < self.indexArray.count; i++){
if ([[self.indexArray objectAtIndex:i] integerValue] == self.selectedIndex){
containsSelected = YES;
break;
}
}
if (containsSelected) {
NSLog(#"execute if in the array");
} else {
NSLog(#"execute if not in the array");
}
I believe this could be further simplifed to:
if ([self.indexArray containsObject:#(self.selectedIndex)]) {
NSLog(#"execute if in the array");
} else {
NSLog(#"execute if not in the array");
}
but I haven't tested it.
I have an NSMutable array which i'm trying to remove objects from and by that to decrease
the count of number of objects on the array, and the count always stays the same,
Meaning, the remove is not working.
Here is the code (it's from the online stanford IOS development course):
- (NSMutableArray *)cards
{
if (!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
if (atTop) {
[self.cards insertObject:card atIndex:0];
} else {
[self.cards addObject:card];
}
}
- (void)addCard:(Card *)card
{
[self addCard:card atTop:NO];
}
- (Card *)drawRandomCard
{
Card* randomCard = Nil;
NSLog(#"This is the count %d",[self.cards count]);
if ([self.cards count]) {
unsigned index = arc4random() % [self.cards count];
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
The count is always 52, even after removing the objects.
Any ideas on how to fix this?
I want to make 1 array that will hold all of the other arrays objects and will look like this
("052-6224754","03-6475075","02-6753231")
my code is:
-(NSMutableArray*) getRecepientsPhones
{
NSMutableArray* phones = [[NSMutableArray alloc]init];
//scroll all choosed contacts and retrieve phones to nsstring
if([recepientsFromContacts count]>0)
for (int i=0; i<[recepientsFromContacts count]; i++)
{
NSMutableArray* tempArray = [[NSMutableArray alloc]init];
if(![[[recepientsFromContacts objectAtIndex:i]objectForKey:#"CPhones"]isKindOfClass:[NSNull class]])
{
[tempArray addObject:[[recepientsFromContacts objectAtIndex:i]objectForKey:#"CPhones"]];
for(int j = 0; j<[tempArray count];j++)
{
[phones addObject:[tempArray objectAtIndex:j]];
}
}
}
//lets fetch from that contact
if([personRecepient count]>0)
{
if(![[personRecepient objectForKey:#"CellPhone"]isKindOfClass:[NSNull class]])
[phones addObject:[personRecepient objectForKey:#"CellPhone"]];
}
NSLog(#"%#",phones);
return phones;
}
[[recepientsFromContacts objectAtIndex:i]objectForKey:#"CPhones"]
is 1 or more dimension array (it is array of phone numbers per person , person can have more than 1 number)
example: ("052-6224754","03-6475075")
but my function returns
("052-6224754","03-6475075"),("02-6753231")
which is not good , what should I do to make it 1 array
("052-6224754","03-6475075","02-6753231")
You should change the line
[phones addObject:[tempArray objectAtIndex:j]];
to
[phones addObjectsFromArray:[tempArray objectAtIndex:j]];
This should result in a flattened array of phone numbers.
Then you should head over to codereview.stackexchange.com because there are several issues with your code fragment.
Edit: Here's a cleaned up version of the method:
- (NSArray *)recepientsPhoneNumbers
{
NSMutableArray* phoneNumbers = [NSMutableArray array];
for (NSDictionary *dict in _recepientsFromContacts)
{
id recipientPhoneNumbers = dict[#"CPhones"];
if (recipientPhoneNumbers != [NSNull null])
[phoneNumbers addObjectsFromArray:recipientPhoneNumbers];
}
id recipientPhoneNumbers = _personRecepient[#"CellPhone"];
if (recipientPhoneNumbers != [NSNull null])
[phoneNumbers addObjectsFromArray:recipientPhoneNumbers];
NSLog(#"%#", phoneNumbers);
return phoneNumbers;
}
I applied Cocoa coding conventions, so ivars are now prefixed with underscores.
I made the following method to remove doubles, however it doesn't fully work. Any suggestions?
Thank you for the help,
-(NSMutableArray*)removeDuplicateCars:(NSMutableArray*)array{
NSMutableArray *noDuplicates =[[NSMutableArray alloc]init];
for (int i=0; i<[array count]; i++) {
int counter =0;
Car *car =(Car*)[array objectAtIndex:i];
if ([noDuplicates count]==0) {
[noDuplicates addObject:car];
}
for (int i=0; i<[noDuplicates count]; i++) {
Car *car2 =(Car*)[array objectAtIndex:i];
if (![car.name isEqualToString:car2.name]) {
counter++;
}
}
if (counter==[noDuplicates count]) {
[noDuplicates addObject:car];
}
}
NSLog(#"number of results = %i",[noDuplicates count]);
return noDuplicates;
}
Create an array called "addedCars" - you will use it to store the name of each unique car.
In each iteration, use [NSArray containsObject:] to check if the current name has already been added to "addedCars". If not, add the name to "addedCars" and the car to "noDuplicates". Otherwise, skip this item, as it has already been added to noDuplicates.
be sure [isEqual:] and [hash] is implemented as you expected
-(NSMutableArray*)removeDuplicateCars:(NSMutableArray*)array{
NSOrderedSet *set = [[NSOrderedSet alloc] initWithArray:array];
NSMutableArray *newArr = [NSMutableArray arrayWithCapacity:[set count]];
for (id obj in set) {
[newArr addObject:obj];
}
return newArr;
}
You used ![car.name isEqualToString:car2.name] to compare objects so I believe you want to filter objects with same name? Than you need to override [isEqual:] for Car
- (BOOL)isEqual:(id)other {
if ([other isKindOfClass:[self class]]) {
return [self.name isEuqalToString: [other name]];
}
return NO;
}
- (NSUInteger)hash {
return [self.name hash];
}
also check this question The best way to remove duplicate values from NSMutableArray in Objective-C?
Im trying to implement UILocalizedIndexCollection as seen on
http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/UILocalizedIndexedCollation.html
I get my address book after obtaining permission from the user which gives me an array of ABRecordRef's, and I have the following code to attempt to build the array of section arrays I need. However I have no idea what to put for collationStringSelectior. Any help would be amazing.
- (void)setListContent:(NSArray *)inListContent
{
if (listContent == inListContent) {
return;
}
[listContent release]; listContent = [inListContent retain];
NSMutableArray *sections = [NSMutableArray array];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
for (int i=0;i < [listContent count];i++) {
ABRecordRef person=[listContent objectAtIndex:i];
NSLog(#"person name is ");
NSString* name = (NSString *)(ABRecordCopyCompositeName(person));
NSLog(name);
NSInteger section = [collation sectionForObject:person collationStringSelector:#selector(????)];
[sections addObject:person toSubarrayAtIndex:section];
}
NSInteger section = 0;
for (section = 0; section < [sections count]; section++) {
NSArray *sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section]
collationStringSelector:#selector(name)];
[sections replaceObjectAtIndex:section withObject:sortedSubarray];
}
[sectionedListContent release];
sectionedListContent = [sections retain];
}
You should use this code:
NSInteger section = [collation sectionForObject:name collationStringSelector:#selector(self)];