Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to append data to NSArray and than get count of data in NArray and retrieve data at specific index.
I am adding data as follows:
NSArray *oneInfo = #[
#{#"trackTime" :theTrack[#"seconds"],
#"trackPrice":theTrack[#"price" ],
#"trackWait" :theTrack[#"wait" ]
}
];
Your array is an array literal which means it is immutable. In order to make an array that you can change, do this:
NSArray *oneInfo = #[#{#"trackTime":theTrack[#"seconds"],#"trackPrice":theTrack[#"price"],#"trackWait":theTrack[#"wait"]}];
NSMutableArray* somethingICanChange = [oneInfo mutableCopy];
[somethingICanChange addObject: moreData];
Note that, if you are not using ARC (why not?), somethingICanChange is an array that you own and needs to be released or autoreleased when you are done with it.
You can not append NSArray you have to create NSMutableArray for appending and other changes your need.
oneInfo = [oneInfo arrayByAddingObject: something];
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I follow the online way to write singleton, each into SecondViewController, address of the print is the same , but why UISwitch interface is initialized ? I was into the original UIViewController? someone was also suggested to write helper class. Everthing is beginning, it is not very clear, Is there any Demo or explain the basic principles?How to write the code depend on second image?
enter image description here
enter image description here
Try this code
static SecondViewController *sharedVCInstance = nil;
+ (SecondViewController *)sharedInstance
{
static dispatch_once_t onceToken = 0;
dispatch_once (&onceToken, ^{
if(sharedVCInstance == nil)
sharedVCInstance = [[SecondViewController alloc] init];
});
return sharedVCInstance;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to use photos Framework (iOS 8.0)? I wanna get one result that can show one by one image in the UICollectionView.
If you want a fetchResult with all images, you can solve it like this:
// Property in your UICollectionViewController.h file
#property (strong, nonatomic) PHFetchResult *allPhotosFetchResult;
// In your awakeFromNib or where you refresh your Data
PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:NO]];
self.allPhotosFetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
In your collectionView:cellForItemAtIndexPath: method you can access the asset with
self.allPhotosFetchResult[indexPath.row];
To get the number of rows in collectionView:numberOfItemsInSection:
self.allPhotosFetchResult.count
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I'm currently making a calorie tracker. In this app, you enter the recent meal you ate, and the amount of calories in that meal. Then you press the "add" button. This button will save the data to a TableView called HistoryTableViewController. To check if the person has left a textfield blank, I have 3 "if statement". One looks like this:
if ([[self.amountText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] < 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"You have not entered the amount of Calories!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[amountHistory removeObjectAtIndex:0];
}
There is one line of code that I know is wrong which is the
[amountHistory removeObjectAtIndex:0];
I don't necessarily want to remove the first object on the tableview, I just want to make sure that the item doesn't get added.
Thanks in advance.
EDIT:
I add an object to the array at the top of the if statements:
total+= self.amountText.text.intValue;
self.totalLabel.text = [NSString stringWithFormat:#"%d", total];
NSNumber *calorieNumber = [NSNumber numberWithInt:self.amountText.text.intValue];
NSString *foodString = nameOfTheFood.text;
NSString *historyString = [NSString stringWithFormat:#"%# Calories in %#", calorieNumber, foodString];
[amountHistory addObject:historyString];
Sounds like you are thinking about it from the wrong angle.
Instead of adding the item to your array and then removing it if it is invalid, you should ensure the item is only added when it is valid.
So in your method you would only add the item at the end, after all validation has passed. You would add early return's in your if statements if the any validation fails so that you don't reach the code that adds the item to the array
The answer to your subject:
How to remove a specific object from an array
is
[array removeObject:obj];
Note, this uses the isEqual message on the object to determine equality. If you want specific object equality, use removeObjectIdenticalTo: instead.
Source: Apple Docs on NSMutableArray
If you are looking to remove the last item in the array use:
[amountHistory removeLastObject];
But if your issue is that you don't want to add blank items to the array why not check if they are blank before adding them?
You can only remove objects from an array if you are using NSMutableArray. Have a read of the NSMutableArray Class Reference
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an NSArray declared like this in my ViewDidLoad
animalz = [NSArray arrayWithObjects:#"rabbit",#"deer",#"ox", #"horse", #"cow", nil]; and I have a UIView subclass that I wrote which takes an NSString and rearranges it. The user presses a button and the number associated with the button is the number in the array that I need (Ex. if the number is three then I would need the value "horse" but if the button was 4 then I would need "cow"); Currently these numbers come in the form of long int and I can't get the corresponding value in the nsarray. I tried doing:
selectedani //this is the long int that represents the button
int indexani = [animalz objectAtIndex:selectedani];
NSString *anistr = [NSString stringWithFormat:#"%i",indexani];
[self rearrange:btistr];
This doesn't give me any compiler warnings or errors but when I press the button , the app crashes. What am I doing wrong and how can I fix it?
selectedani //this is the long int that represents the button
NSString *anistr = [animalz objectAtIndex:selectedani];
[self rearrange:anistr];
animalz is an array of NSString objects, but when you call [animalz objectAtIndex:selectedani], you assign the result to an int.
First of all, did you assigned selectedani before you used it? If not then you shouldn't use selectedani. It maybe the memory location or some other property of the button which may vary.
[animalz objectAtIndex:selectedani]
is returned an object, casting this object to an int not what you want and of course it's not an index too.
What can you do? Assign a tag number to the corresponding button dynamically, like
button.tag = 1;//or something like it
Then in the IBAction method retrieve the tag and use it to parse your array.
- (IBAction)buttonPressed:(id)sender
{
UIButton *pressedBtn = (UIButtton *)sender;
NSString *anistr = [animalz objectAtIndex:pressedBtn.tag];
//do what you want to do with the string
[self rearrange:anistr];
}
In this case, declare animalz as an iVar or property.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to remove from NSMutableArray while iterating?
Mods: I realized this was a duplicate of this question, can you close/delete it?
If you don't want to create a temporary or new array, you can use this:
NSMutableArray *array = ...; // your mutable array
NSIndexSet *toBeRemoved = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
// The block is called for each object in the array.
BOOL removeIt = ...; // YES if the element should be removed and NO otherwise
return removeIt;
}];
[array removeObjectsAtIndexes:toBeRemoved];
(But a temporary NSIndexSet is used.)
Remark: My thought was that it might be more efficient to remove all matching elements "at once" instead of removing each element separately during the iteration, as suggested by Richard J. Ross III.
But a short test showed that this is not the case. Removing every second element from an array with 1000000 elements takes almost the same time with both methods on my computer.
You can use an NSPredicate to do this, see this SO question and answer.
https://stackoverflow.com/a/129942/36984
And the Apple docs for this:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/filterUsingPredicate: