English 中文(简体)
阵列内的变化价值
原标题:Change Values inside an array

For example: I have a mutable array of BOOLs(or [NSNumber numberWithBool:TRUE]; ). and then I want to change, for example the first value in this array to FALSE.

[array addObject:[NSNumber numberWithBool:TRUE]];
[array objectAtIndex:0] ??? 

我知道,完成这项任务的方法非常奇怪。

[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:FALSE]];

Yes, this fully satisfies me, but I am looking for another way, that is more simple than this. Something like [[array objectAtIndex:0] setBoolValue:FALSE]; for example, imagine, if I had UIButton instead of BOOLs: [[array objectAtIndex:0] setHidden:TRUE]; or this is only dreams?

提前感谢!

最佳回答

The best way to do this is

[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:FALSE]];

原因是[array ObjectAtIndex:0] 返回NSNumber,与您有一系列的不同。 UIButtons, which would re a UIButton Object that You can act on. 由于您重新获得<代码>NSNumber,你可以做任何事情做。 NSNumber <>/code> direct since it doesn t have the methods sent to the NSNumber

问题回答

没有任何东西阻止你向一个阵列中的物体发送突变电文,但NSNumber根本无法变。 因此,你不得不用不同的标语取代标的。 它与阵列或语言毫不相干,你可以改变国家独立党。

Something to understand here: Arrays are simple collections of pointers to object. Hence, you can easily to things like
((UIButton*)[array objectAtIndex:0]).hidden=TRUE;
Modifying the object is possible and often done. But exchanging one object for another one, you need to use Replacement techniques as you pointed out. However, simpler objects are, as said above by @Chuck, simply not mutable (like NSNumber).

并请指出,我前面的法典在稳定方面是可怕的。 你们怎么肯定知道,它把你从你们的魔术帽(即阵列)中夺走。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...