assigning a value to an object's iVar inside an array [duplicate] - ios

This question already has answers here:
Property '' not found on object of type 'id'
(2 answers)
Closed 6 years ago.
NSMutableArray *dataArray = [NSMutableArray array];
for (int i = 0; i<5; i++){
[dataArray addObject:[UITextView new]];
dataArray[i].text = "here";
^Property 'text' not found on object of type 'id'
}
I check the class, methods and iVars... everything is there, but I can't use them.
I want a UIScrollView with a user defined number of data fields (like 'Contants' app). So I load them into an array and put them onto the scroll view.
However, I can't call the methods. I want to use some kind of strut (array, dict, etc...)
I tried this:
textView1 = dataArray[i];
textView1.text = #"######### Here I am ##########";
But that doesn't seem to store in the object inside the array. I thought the array stored a pointer to the object and that textView1 would be a pointer to the object so it should store the value of '.text' to the same object.

What you tried seems a bit unclear to me, but this should work:
NSMutableArray *dataArray = [NSMutableArray array];
for (int i = 0; i<5; i++){
UITextView *t = [UITextView new];
[dataArray addObject:t];
t.text = #"here";
}

Related

Can't add NSNumber object to NSMutableArray [duplicate]

This question already has answers here:
Having trouble adding objects to NSMutableArray in Objective C
(2 answers)
Closed 6 years ago.
- (void)setToInitialStateMain
{
[super clearBoard];
if (_data[#"StoneOne"] != nil) {
NSDictionary* stoneOne = _data[#"StoneOne"];
NSNumber* c = stoneOne[#"Column"];
NSNumber* r = stoneOne[#"Row"];
NSInteger column = [c intValue];
NSInteger row = [r intValue];
[_boardCol addObject:[NSNumber numberWithInt:column]];
[_boardRow addObject:[NSNumber numberWithInt:row]];
}
}
So the #"StoneOne", #"Column", and #"Row" keys are coming from an NSDictionary plist file. When I try to convert the NSNumber #"Column" to NSInteger, everything works ok.
Now, the line [_boardCol addObject:[NSNumber numberWithInt:column]]; is ok in terms of 'column' being the correct integer (2). But, when setup a breakpoint at the end of the method call to examine _boardCol (as well as _boardRow), both NSMutableArray* instance variables are reading nil. Why is that?
All instance variables start out as nil. And a method call to nil returns nil (or does nothing). You need to instantiate the NSMutableArray yourself at some point. This is often done in -init, or inline code checking if the ivar is nil and if so allocating it:
if (self.boardCol == nil) {
self.boardCol = [[NSMutableArray alloc] init];
}
The above is not a safe thing to do if multiple threads could be involved. It's often easier to just create them in your -init method.

How to modify string element of a NSArray using fast enumeration? [duplicate]

This question already has answers here:
Avoiding "NSArray was mutated while being enumerated"
(11 answers)
Closed 8 years ago.
Can anyone pls suggest how to string element of an NSArray using fast enumeration. Its very easy to do the same with normal for loop. Below code works fine
_tempArray = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F", nil];
NSUInteger i=0;
for (; i<[_tempArray count]; i++) {
if ([[_tempArray objectAtIndex:i] isEqualToString:#"A"]) {
[_tempArray replaceObjectAtIndex:i withObject:#"a"];
}
}
I want to do the same as above code does with fast enumeration. I tried the below code but it gives *****Error: Collection <__NSArrayM: 0x610000043090> was mutated while being enumerated.******, which means it is not possible to modify the collection while enumerating it. Can anyone suggest any way to achieve it.**
NSUInteger j=0;
for (NSString *temp in _tempArray) {
if ([temp isEqualToString:#"A"]) {
[_tempArray replaceObjectAtIndex:j withObject:#"a"];
}
j++;
}
How to modify string element of a NSArray using fast enumeration?
Don't. You are not supposed to modify the array during enumeration. It's not an arbitrary restriction -- doing so may result in logical errors.
If you want to modify/transform an array, then use an explicitly indexed for loop instead.
You can tighten up the code with the array indexing syntax:
for (int i=0; i<_tempArray.count; i++) {
if ([_tempArray[i] isEqualToString:#"A"]) {
_tempArray[i] = #"a";
}
}

Add array to second column of another array

I'm new at Objective C. I have a list of questions, and each question has multiple answers, so I need to have an array which contains another array with a question number, then an array of answers.
I have an NSMutableArray which I'm instantiating it with this line:
_randomQuestionNumberArray = [[NSMutableArray alloc] initWithCapacity:2];
I fill this array with numbers:
for (int i = 0; i < numberOfQuestions; i++) {
NSNumber* xWrapped = [NSNumber numberWithInt:i];
[_randomQuestionNumberArray addObject:xWrapped];
}
Then I have another NSMutableArray which is just a regular array of numbers. I want to add this array to the second column of _randomQuestionNumberArray at a specific row. I'm using this code for this but it doesn't seem to work properly.
[_randomQuestionNumberArray insertObject:_tempAnswers atIndex:position];
Can anyone offer solution to this? Thank you!
Don't use an array of arrays. It will work initially, but it isn't flexible or clear (so hard to maintain).
Instead, use an array of dictionaries, where each dictionary has a number of keys:
_randomQuestionNumberArray = [NSMutableArray array];
for (int i = 0; i < numberOfQuestions; i++) {
[_randomQuestionNumberArray addObject:[#{ #"questionNumber" : #(i) } mutableCopy]];
}
then, when you have your answers:
NSMutableDictionary *questionDict = [_randomQuestionNumberArray objectAtIndex:...];
[questionDict setObject:_tempAnswers forKey:#"answers"];
and now it's obvious what each piece of information is for.
Note: the index of each item in the array could work as the question number if you wanted it to...
Try using this:
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
in place of anObject put your array.

Converting NSString to object name [duplicate]

This question already has answers here:
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 8 years ago.
NSString *q = #"Day";
NSArray *shu = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%#1",q];
[[self.shu objectAtIndex:0] setText: #"HI"];
In the following code I have an array that I am filling up with NSStrings. What I am trying to do is fill up the array with a UITextfield objects not a string of the name. Is it possible to convert a string to a object UITextfield? Something like this:
NSString *q = #"Day";
NSArray *shu = [NSArray arrayWithObjects:[NSObject namewithFormat:#"%#1",q];
[[self.shu objectAtIndex:0] setText: #"HI"];
EDIT:
What I want to do is using this code:
for (int i=1; i<=5; ++i) {
NSArray *shu = [NSArray arrayWithObjects:[NSString stringWithFormat:#"TextField%d1",i],[NSString stringWithFormat:#"TextField%d2",i];
}
Make it so that when i = 1 then:
NSArray *shu = #[Textfield11,Textfield12];
And when i = 2 then:
NSArray *shu = #[Textfield21,Textfield22];
If I understand what you're asking for, you want to set a property of a view controller based on it's name.
You should look into key value coding. There are methods setValue:forKey and setValue:forKeyPath.
setValue:forKey attempts to find a property or instance variable of the target object and change the value of that proerty.
setValue:forKeyPath will traverse a list of objects. So, for example, if you have a view controller called fooVC, and it has a text field called fooTextField.
you could use code like this:
[fooVC setValue: #"HI" forKeyPath: "fooTextField.text"];
That would search for a property called "fooTextField" in the object "fooVC", and then search for a property called "text" in the "fooTextField" object. If both exist, it would set the value of the text property to the string "HI".

Can I create a pointer to an object using its name in a string variable? [duplicate]

This question already has answers here:
Can I do NSVariableFromString like NSClassFromString and NSSelectorFromString?
(3 answers)
Closed 9 years ago.
In essence, what I want to do is to has a string I've created as the name for a variable. However with the code below I understandably get a redefinition error.
NSString *pointerName = #"myPointer";
NSArray *pointerName = [NSArray array];
Is there a way to use the string as its content rather than having the compiler think I mean that's the name of the new object I'm trying to create?
Instead of store string value for pointer. You can use like this
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for (int i = 0; i < [array1 count]; i++)
{
[dict setObject:[NSArray array] forKey:[array1 objectAtIndex:i]];
}
I hope it is useful for you..
According to cocoa convention you cannot create the pointer of an object of the same name variable. Also the apple doc says the same thing please refer the link https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html

Resources