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".
Related
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.
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";
}
This question already has answers here:
How to Print out(NSLog) the properties of a custom object added to a NSMutableArray
(4 answers)
Closed 8 years ago.
I create the instance of the Object 'Event' then add it to the array called eventsStore.
Event *event = [[Event alloc] init];
event.courseName = #"Maths";
event.room = #"405";
event.startTime = [NSNumber numberWithInt:9];
event.endTime = [NSNumber numberWithInt:10];
[eventsStore addObject:event];
I can see in the debugger when I run it that all the information is stored but when I try to NSLog the information back out using:
NSLog(#"%#", [eventsStore objectAtIndex:i]);
The only thing that shows up <Event: 0x10010ff10>. How can I print out the whole contents of the Event?
To get a more detailed output when using this kind of NSLog, you can override the - (NSString *)description method of your NSObject subclass. See also this question: What is the Objective-C equivalent for "toString()", for use with NSLog?
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
This question already has answers here:
Getting an NSArray of a single attribute from an NSArray
(3 answers)
Closed 9 years ago.
I have the following issue, I need to get an array of values from an array of custom Objects. Is there a method for doing this without iterating the principal array, let me graphic it a little bit.
NSArray *principalArray = #[
customObject1,customObject2,customObject3,....customObject(n)
];
This customObject instances have a properties lets say id,name,lastname.
I want to get an NSArray with the value of name from the principalArray
Thanks for your help.
EDIT:
As somebody pointed out in comments: its a duplicate of existing SO question: Getting an NSArray of a single attribute from an NSArray
There is a method for NSArray - valueForKey - with key being an attribute of your first array. This method returns you an NSArray from an NSArray.
In your case you can do the following:
NSArray *nameArray = [principalArray valueForKey:#"name"];