I would like to create a shorter and more elegant version of some working code.
FYI I have to add an element to an existing array, and then remove one, and repeat. (unfortunately I can't add more than one element and then remove more than one).
Working code:
NSMutableArray *destinationArray;
destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// Copy one specific array element from originArray to destinationArray
// Note: assume originArray and destinationArray populated at this point
int originIndex = var1-var2+1;
[destinationArray addObject:originArray[originIndex]];
// Remove one specific array element from destinationArray
[destinationIndex removeAllIndexes]; // clear index if used previously
[destinationIndex addIndex:var3-var4-1];
[destinationArray removeObjectsAtIndexes:destinationIndex];
I would prefer some version of the following:
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectsAtIndexes:[var3-var4-1]];
Is there a way to make this shorter version work? (gives expected identifier error)
Thanks much for your help...
Why not do this:
NSMutableArray *destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// somewhere in here objects are added to "originArray"
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectAtIndex:var3-var4-1];
Related
In Xcode 7, I'm getting this warning:
Null passed to a callee that requires a non-null argument
.. from this nil initialization of a NSMutableArray...
sectionTitles = [[NSMutableArray alloc] initWithObjects:nil];
I've found that I should be using removeAllObjects instead.
[sectionTitles removeAllObjects];
However, this doesn't allow me to evaluate a sectionTitles.count == 0. I did try sectionTitles == nil, however unless I use iniWithObjects I can't add objects later on.
I need to set the array to nil or zero, when I refresh my datasource, when there's no records. I don't seem to be able to use addObject to add items unless I've used initWithObjects.
Passing non-null parameters is only partly the answer.
The new Objective-C nullability annotations have huge benefits for code on the Swift side of the fence, but there's a substantial gain here even without writing a line of Swift. Pointers marked as nonnull will now give a hint during autocomplete and yield a warning if sent nil instead of a proper pointer.
Read NSHipster comprehensive article.
In oder to take advantage of the same contract in your own code, use nonnull or nullable:
Obj-C
- (nullable Photo *)photoForLocation:(nonnull Location *)location
Why don't you try:
sectionTitles = [[NSMutableArray alloc] init];
or any of the following:
sectionTitles = [[NSMutableArray alloc] initWithCapacity:sectionTitles.count];
sectionTitles = [NSMutableArray new];
sectionTitles = [NSMutableArray array];
sectionTitles = [NSMutableArray arrayWithCapacity:sectionTitles.count];
maybe some silly ones:
sectionTitles = [NSMutableArray arrayWithArray:#[]];
sectionTitles = [#[] mutableCopy];
There are lots of ways to create empty mutable arrays. Just read the doc.
Got the same error when initializing an NSMutableArray with zeros,
[[NSMutableArray alloc] initWithObjects:0, 0, 0, 0, 0, nil];
Changed it to
[NSMutableArray arrayWithArray:#[#0, #0, #0, #0, #0]];
I'm trying to rewrite one element from self.tableData to another.
my NSMutableArray:
self.tableData = [[NSMutableArray alloc] initWithObjects:
[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"],
[[Cell alloc] initWithName:#"Kabanos" andImage:#"spodwwdwdrt.jpg" andDescription:#"dwdw" andTypes:#"dwdw dww" andforWho:#"dawwd"],
[[Cell alloc] initWithName:#"dwwd" andImage:#"dwwd" andDescription:#"dwwd" andTypes:#"wdwd daww" andforWho:#"dadawwa"],nil];
NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];
But it's not working, maybe it's a newbie question but i have never before worked with arrays with many objects inside.
With self.tableData[0] i men rewrite object
[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"],
NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];
The problem with the code above is that you haven't created an array for newArray to point to, so the value of newArray is nil. Do this instead:
NSMutableArray *newarray = [NSMutableArray array];
[newarray addObject:self.tableData[0]];
Now newArray will point to a valid mutable array to which you can add objects.
Also, realize that even with the fixed code, newArray[0] will point to the very same object that you've stored in self.tableData[0], not a copy. If you want it to point to a different object that contains similar data, you should either make a copy of the object or instantiate a new one, e.g.:
[newarray addObject:[self.tableData[0] copy]];
or:
[newarray addObject:[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"]];
You need to init new array. You can do it like so:
NSMutableArray *newarray = [NSMutableArray array];
The problem here is you didn't initialize newarray. You need to do
newarray = [NSMutableArray array];
But also note that adding self.tableData[0] to it is different from adding another alloc'ed and init'ed Cell to it because the former increases the reference count on self.tableData[0], while the latter creates a new Cell object.
This also means that if you make a new Cell object and add it to the mutable array, and later on modified that object in the mutable array, it wouldn't change the cell in the first array. But if you did it the first way, it would.
i am constructing All data to look like a response data dictionary from a server.
Now, newsFeedsDict1 which should Dictionary for both Bolly and Global is not only showing all data inside Global dictionary only. While my for loop is running its showing correct data for Bolly. but for 2nd time its showing Bolly's data also in Global dictionary.
if(internetStatus == NotReachable)
{
NSMutableArray *titleArr = [[NSMutableArray alloc] init];
NSMutableArray *wholeFeeds = [[[NSMutableArray alloc] init] autorelease];
[titleArr addObject:#"Bolly"];
[titleArr addObject:#"Global"];
for (NSString *title in titleArr) {
//titleArr = [[NSUserDefaults standardUserDefaults] objectForKey:#"TitleArray"];
NSLog(#"TITle arr %#",titleArr);
NSLog(#"No internet");
OrderedDictionary *newsFeedsDict1 = [[[OrderedDictionary alloc] init] autorelease];
NSMutableDictionary *newsFeedsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
NSMutableDictionary *articleDict = [[[NSMutableDictionary alloc] init] autorelease];
[articleDict setObject:myLocalArray forKey:#"article"];
[newsFeedsDict setObject:articleDict forKey:#"Articles"];
[newsFeedsDict setObject:title forKey:#"#name"];
[newsFeedsDict1 setObject:newsFeedsDict forKey:title];
[wholeFeeds addObject:newsFeedsDict1];
NSLog(#"news feed dict %#",newsFeedsDict1);
NSMutableDictionary *temparticleDictionary = [[NSMutableDictionary alloc] init];
self.articleDictionary = temparticleDictionary;
self.categoriesDictionary = [[NSMutableDictionary alloc] init];
self.categoriesDictionary =newsFeedsDict1;
[self createArticleDictionaryForCategory:newsFeedsDict];
}
}
It's difficult to tell what your code is supposed to do, and how you can tell that one dictionary has the same content as another. A couple problems:
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
The first line is entirely unnecessary. You're creating a new array, assigning it to myLocalArray, and then assigning something else to myLocalArray. You do the same later with self.categoriesDictionary. This leads me to believe that you have some misunderstanding with respect to object pointers.
So, the array that you get from your shared database ends up at myLocalArray, and you then add that array to the dictionary at articleDict, and then add articleDict to newsFeedDict and in turn add that to newsFeedDict1. And then you log newsFeedDict1. You do exactly the same for both your titles, #"Bolly" and #"Global", so it's not at all surprising that you see the same output in both cases.
I'm having a hard time seeing why you expect the output to be different, and I have to guess that again it's due to a misunderstanding of what happens when you assign one object pointer to another. Or, perhaps you're expecting the array that you get from [[Database sharedDatabase] getArticleData:title]; to be different because you're passing in different titles. Maybe you really should be getting different arrays there; it would be a good idea to look at what happens in that -getArticleData: method and whether it really is giving you the right results for each title.
If that doesn't help, take some time to clean up your code so that it's easier for us, and more importantly, for you to really see what's going on. Also, please edit your question to give a better description of what you're seeing and how that's different from what you expect to see.
Can you write the snippet for *getArticleData()* method
arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
and
arrayOfElements = [[NSMutableArray alloc] init];
arrayOfElements = [someObj getArray];
What's the difference?
The first arrayOfElements does not seem to lose its objects when it returns count in numberOfRowsInSection:(NSInteger)section, but the second one does. I get EXC_BAD_ACCESS when I do it the second way.
EDIT:
Can I suppose now that this is the best way,
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];
because I am initializing an array with the contents of whatever will be autorelease'd, and I now have a fully independent array in the current class, that is viewDidLoad, oops sorry, ViewController.
This line creates an NSMutableArray from an existing array
arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
This combination first creates an NSMutableArray and then instantly discards it replacing it with what is returned by [someObj getArray]
arrayOfElements = [[NSMutableArray alloc] init]; // Create new NSMutableArray
arrayOfElements = [someObj getArray]; // Throw away the newly created array and replace with the result of [someObj getArray]
If you are not using ARC then it is purely by luck that either would work.
In both cases arrayOfElements is being assigned an autorelease'd object - which will be cleared soon (most likely the next runloop). It is only by chance that nothing else has been written over this point of memory which allows one of your implementations to still work.
If you are not using ARC then really you should update your project to be using it will handle a lot of cases like this for you.
You should definitely be using properties (not bare ivars) as this will help reduce memory issues (for non-ARC) and give a more consistent interface to your code.
In your header (or class extension) declare the property like this
#property (nonatomic, strong) NSMutableArray *arrayOfElements;
Now for ARC you can simple do
[self setArrayOfElements:[[someObj getArray] mutableCopy];
for non-ARC you can do
NSMutableArray *array = [[someObj getArray] mutableCopy];
[self setArrayOfElements:array];
[array release]; array = nil;
Also note that getArray is a bad method name.
The use of “get” is unnecessary, unless one or more values are returned indirectly.
Coding Guidelines
When you are adding objects to mutable array from another array, try this:
[arrayOfElements addObjectsFromArray: [someObj getArray]];
If you're not using ARC, you need to make sure its retained.
if (arrayOfElements) {
[arrayOfElements release];
}
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];
I am from android background and I just started working on iPhone
I want to perform this operation in iPhone as I do in Android.
ArrayList<String> aa = new ArrayList<String>();
public void fillArray(String s)
{
aa.add(s);
}
As Binyamin Sharet suggest you have to use NSMutableArray. This class allows you to create a dynamic array. You can perform addition or deletion. On the contrary NSArray is a immutable version of it. You cannot add or delete objects to a NSArray once created.
The same distinction can be applied to NSDictionary and NSMutableDictionary (and other).
Here a simple example.
NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:#"first string"];
[arr addObject:#"second string"];
An important aspect of NSMutableArray (the same could be applied to other class) is the memory management one. When you add an object to a NSMutableArray it retains objects added to it. So, if you NOT use ARC you have to deal with this aspect.
NSMutableArray* arr = [[NSMutableArray alloc] init];
id obj = [[NSObject alloc] init]; // a general object
[arr addObject:obj];
[obj release];
For further info I suggest you to read about NSMutableArray class reference.
Hope it helps.