Getting a list of all object properties [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get an object attributes list in Objective-C
I am trying to find a way to iterate through all properties on an object and assign the first non empty property's value to another variable. All these properties are string and i am trying to use isEqualToString:#"" to check each property manually before assigning the first non empty one to another variable. Is there a way to do this efficiently by iterating through each property instead of having if conditionals checking each property individually?

Giuseppe's answer is good but it seems you don't need to complicate yourself with that.
If you say all propertys are strings, why not use an NSArray and ,using fast enumeration, check if the string is in the array , if not just add it. That way you don't have a limit (in case you need to modify the project later).
Also this could work even with different types of variables/propertys. When you loop through the array just use isSubclassOfClass: to check type.

Related

Accessing variables with related names in a for loop [duplicate]

This question already has an answer here:
Syntax help - Variable as object name [duplicate]
(1 answer)
Closed 8 years ago.
I have three NSRects in separate variables named rect1, rect2, and rect3, and I want to increment each one's origin.x by 10.
I thought a for-loop would be good for this, but I don't know how I can do it.
This is an example of how I want it to work:
for(int i=0, i<3, i++) {
rect[i].origin.x +=10
}
but this exact code gives an error
property "rect" not found on object of type "graphics"
Is there a way to code it like in my example?
Is there a way to increment a variable with for loop
Not in objective-c for sure. You cannot declare variables in 0-n format and loop through it.
If you create an array out of your NSRect variables then you can loop through the array and modify properties.
Note: NSArray can only hold Objects not primitives, you might find this handy

Is there any way to get a property's type in Objective-C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect a property return type in Objective-C
Is there any way to get a property's type in Objective-C? I can access the property like this:
objc_property_t* properties = class_copyPropertyList(cl, &count);
And get the name like this:
property_getName(properties[i]);
What I need to do though is get the type. Also, the value will be nil in most cases so I can't just get the value calling object_getClass().
Not in the sense that you seem to be asking — in Objective-C classes are typeless; when dealing with non-C types beyond 'it's a class' the type of properties isn't known at runtime. That's why if you do something silly like the following in a view controller:
[self setValue:#3 forKey:#"view"];
You'll see an exception raised when the controller attempts to send a view message to the NSNumber rather than by the key-value coding mechanisms because you tried to put something that isn't a view into in an inappropriate property.
Parsing property_getAttributes will allow you to go no further than distinguishing the various C literal types from an Objective-C object type.

Access objects of a specific type using for-in loop in Objective C [duplicate]

This question already has answers here:
Fast Enumeration on NSArray of Different Types
(5 answers)
Closed 8 years ago.
I am running a for-in loop over an NSMutableArray. There are instances of Class A in the array also out of those some are actually instances of its subclass B.
So If I only want members of subclass B, I am checking the class of each object I get in an if condition inside the loop body.
Is it possible that instead of writing something like this,
for(A* obj in collection){
if([obj isKindOfClass:[B class]]){
//take some action.
}
}
I can do something like this?
for(B* obj in collection){
//take some action.
}
Will I get the same result?
To my knowledge: no.
The for each loop will traverse every object in the collection, and I don't think you can specify that you only want to traverse a specific type.
To be clearer: The Object you specify: for (MyObject* obj){..} is a type-cast. So you're telling the object in the collection that they should be/behave as MyObject
Can I do something like this...
No, you can't use the type of the index variable in fast enumeration to select only some of the objects in the collection. If the collection contains different types of objects, you'll have to test each object first. Instead of testing class membership, though, it's often nicer to test for the behavior you're looking for with -respondsToSelector: or -conformsToProtocol instead of -isKindOfClass:.

How to show calculated values in NSTableView?

I have an NSTableView that binds via an NSArrayController to an NSMutableArray of NSString's.
There's also a TextView in the same window. What I want to do is display a number of occurrences of each NSString in the NSTableView.
I've thought of a way to do this, but that doesn't look elegant way of doing this:
Inherit from NSString and define new method that performs a search in predefined object (NSTextView) and returns the number of occurrences.
I'm guessing there must be a more natural way of achieving the same result?
EDIT:
Sorry, should have clarified. NSSMutableArray is an array of NSObjects that have an NSString property. I suppose I could define an extra method (findAllOccurencesOfString:inString:) which would return a number. But the question is how do I bind to this function and in that binding how to I pass a var (pointer to textField)??
You'll need to have a wordCount (read only) property on whatever objects are in your table data source, this will have to call your new method internally using the object's own string value, as you can't pass parameters in bindings (unless they've changed, I haven't used bindings for a while as I've been concentrating on iOS). Then bind to this property for the column in the table. Presumably you don't need to pass the pointer to the textfield as there is only one?

How to do string manipulation in an NSValueTransformer?

I have a table column that I'm binding to a value in an NSArrayController. What I'm trying to do is display only a substring of the actual value in the array controller. The way I've been trying to do that so far is by creating an NSValueTransformer subclass, and then doing the string manipulation in the transformedValue method. However, I can't figure out how to get the incoming value turned into a string (it's of type NSConcreteValue), and maybe there's an easier way to do this without value transformers.
Sounds like you're doing presentation-side formatting, in which case you should be using a formatter instead.
Then again, if this is a string containing multiple values (e.g., something like “from 42 to 100”), you should make a model object from it instead, and store those in the array controller. Then you can bind your table columns to specific properties of the model objects, and not have to worry about picking the string apart and then reassembling it (except when you load and later save the model).
Edit: Never mind; I didn't see that the object values are NSValues, not NSStrings.
You can get a string representation of any object using the -description method, but for instances of NSValue it's unlikely to print anything especially meaningful. In other words, it's up to your value transformer to interpret the passed-in object and produce a string. If it's an NSValue instance, the question is what type of data that instance contains. Once you know that, you can write code to represent it as a string (similar to NSStringFromRect()).