Put all object properties into an NSDictionary or NSArray - iphone

Is there a way to automatically return an NSDictionary of all the (public) properties in a class? You can assume all properties are property lists and that I don't want to just hand over a pointer to the class itself. Any existing magic that can pull this off?
I know I can lazy instantiate an NSDictionary and manually fill it with properties in the getter.

It's easy to get an array of declared properties using the class_copyPropertyList and property_getName functions in the Objective-C runtime. Here's one such implementation:
- (NSArray *)properties
{
NSMutableArray *propList = [NSMutableArray array];
unsigned int numProps = 0;
unsigned int i = 0;
objc_property_t *props = class_copyPropertyList([TestClass class], &numProps);
for (i = 0; i < numProps; i++) {
NSString *prop = [NSString stringWithUTF8String:property_getName(props[i])];
[propList addObject:prop];
}
return [[propList copy] autorelease];
}
You could add this as a method in a category on NSObject. Here's a full code listing that can be compiled that demonstrates this.
I'm not sure how'd you do it with an NSDictionary, only because I'm not sure what you expect the key-value pairs to be.

Related

NSMutableArray of NSMutableArrays. Mutating method sent to immutable object

I have NSMutableArray of NSMutableArrays:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i = 0; i < 5; i++)
{
NSMutableArray *miniArray = [[NSMutableArray alloc]init];
for (int k = 0; k < 30; k++)
{
[miniArray addObject:#"0"];
}
[array addObject:miniArray];
}
Then, when I try to do this:
[[array objectAtIndex:packIndex]replaceObjectAtIndex:index withObject:#"1"];
it crashes with: [__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
Why ? How to fix ? Thanks !
UPD:
I save this array in NSUserDefaults:
[defaults setObject:array forKey:#"mainArray"];
Then, I read it in the other class:
array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"mainArray"]];
Also, I must to mention that sometimes the code runs well and it changes "0" to "1". But it also crashes sometimes. So I cant see the logic, why it works fine or why it crashes sometimes.
The problem is that when you read the array out of NSUserDefaults, the mini-arrays are not automatically NSMutableArrays.
Try this:
array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"mainArray"]];
for(int i = 0; i < array.count; i++) {
NSArray * tempArray = array[i];
array[i] = [tempArray mutableCopy];
}
Edit:
Best Coder's answer explains why this is.
Objects stored in NSUserDefaults are stored as immutable versions, basically NSUserDefaults is a plist and there is no flag marking an array as mutable/immutable so when you read them back out, they are assumed to be immutable.
Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for "MyStringDefault", the string you later retrieve
using stringForKey: will be immutable.
Instead, make a mutableCopy of the array you retrieve from NSUserDefaults, add your object, then set your new array back in.
see this link:
https://developer.apple.com/documentation/foundation/nsuserdefaults
Try using more verbose code:
NSMutableArray *tempArray = [array objectAtIndex:packIndex];
[tempArray replaceObjectAtIndex:index withObject:#"1"];

error in array cleaning method

I am attempting to use this array cleaning method, and there seems to be an error. I can't spot it, I know the array goes in with 3116 items, comes out with 3116 (and I know for a fact there are three duplicates.
Please advice, thanks!
-(NSArray*) removeDuplicates:(NSArray*)inputArray{
NSMutableArray *arrayToClean = [NSMutableArray arrayWithArray:inputArray];
for (int i =0; i<[arrayToClean count]; i++) {
for (int j=(i+1); j < [arrayToClean count]; j++) {
if ([[arrayToClean objectAtIndex:i] isEqual:[arrayToClean
objectAtIndex:j]]) {
[arrayToClean removeObjectAtIndex:j];
j--;
}
}
}
NSArray *arrayToReturn = [NSArray arrayWithArray:arrayToClean];
return arrayToReturn;
}
NSSet will make this a lot easier:
-(NSArray *)removeDuplicates:(NSArray *)inputArray {
NSSet *unique = [NSSet setWithArray:inputArray];
return [unique allObjects];
}
Please note that a set has no guaranteed order. If you need the objects in the array to be in a specific order then you should sort the resulting array as needed.
It may also be appropriate to use an NSSet instead of the original array, then you don't need to worry about duplicates at all. But this depends on the other needs of your array.
Hey You can use another alternative for this.You can use the NSSet here for this task.
NSSet declares the programmatic interface for static sets of distinct objects
You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
You Just need To call below method.
-(NSArray *)removeDuplicates:(NSArray *)inputArray {
NSSet *finalData = [NSSet setWithArray:inputArray];
return [finalData allObjects];
}
If really face any problem in above way of cleaning ducplicates then you can try another Alterantive.
-(NSArray *)removeDuplicates:(NSArray *)inputArray {
NSMutableArray *inputArray1=[NSMutableArray arrayWithArray:inputArray];
NSMutableArray *finalARray=[[NSMutableArray alloc]init];
for (id obj in inputArray1)
{
if (![finalARray containsObject:obj])
{
[finalARray addObject: obj];
}
NSLog(#"new array is %#",finalARray);
}
return finalARray;
}
I hope it may help you ...
Here is a helper function I had in a previous project to do the exact same thing
- (NSMutableArray *)removeDuplicates:(NSMutableArray *)sortedArray{
NSMutableSet* valuesAdded = [NSMutableSet set];
NSMutableArray* filteredArray = [[NSMutableArray alloc] init];
NSString* object;
/* Iterate over the array checking if the value is a member of the set. If its not add it
* to the set and to the returning array. If the value is already a member, skip over it.
*/
for (object in sortedArray){
if (![valuesAdded member:object]){
[valuesAdded addObject:object];
[filteredArray addObject:object];
}
}
return filteredArray;
}

What are the basic differences between these ways creating and handling arrays?

I am unsure which way to declare my arrays with fixed size of 10 objects of type MyClass and what are the implications of these different alternatives for efficiency, ease of coding or anything else.
...keeping in mind the new xCode4.4 features, esp:
For the NSArray and NSDictionary classes, support is provided for
Objective-C literals.
Subscripting using '[ ]' syntax is supported for Objective-C
container objects.
...and of course using ARC
In particular I need to write contructor methods that return these arrays as a result.
Alternative1
MyClass* objectOfMyClass;
MyClass* array1[10];
array1[5] = objectOfMyClass;
Declaration of method:
- (MyClass*[]) createArray { <--- is this declaration correct like this ?
ps. AFAIK these arrays are put on the stack memory - but I am not sure!
Alternative2
MyClass* objectOfMyClass;
NSMutableArray *array2 = [[NSMutableArray alloc] init];
for (int i = 0; i<10; i++)
[array2 addObject:objectOfMyClass]; //objects get added in some way...
//can't directly access nTh object in this case, need to add from 0 to 9
//conversion to non mutable array, since size will not change anymore
NSArray *array3 = [NSArray arrayWithArray:array2];
Declaration of method:
- (NSArray*) createArray {
ps. AFAIK these arrays are put in main memory - not stack - but I am not sure!
Alternative3
NSArray *array4 = [[NSArray alloc] init];
array4 = ...how to prepare the array so it can hold 10 objects without using NSMutableArray ?
otherwise I do not see a difference to alternative 2...
for (int i = 0; i<10; i++)
array4[i] = objectOfMyClass];
Declaration of method:
- (NSArray*) createArray {
Many thanks for bringing light into this!
There is a great article about literals here. You cannot do Alternative 1. The best way to do this is:
NSMutableArray *holdsMyClass = [NSMutableArray arrayWithCapacity:10]; // sized so array does not need to realloc as you add stuff to it
You cannot arbitrarily increase the size of the array by indexing past the size - if you have an object at index 5, you can replace it though:
holdsMyClass[5] = obj;
For example, if you try to compile this, it fails:
- (NSArray*[]) createArray
{
NSArray *foo[10];
foo[2] = [NSArray array];
return foo;
}
generates this error: "array initializer must be an initializer list"

Empty NSMutableArray , not sure why

Ok so I populate the array like this:
NSMutableArray *participants;
for(int i = 0; i < sizeofpm; i++){
NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i];
NSString *pmpart_email = [pmpart_dict objectForKey:#"email"];
NSString *pmpart_email_extra = [#"pm" stringByAppendingString:pmpart_email];
[participants setValue:pmpart_email forKey:pmpart_email_extra];
NSLog(#"%#", participants);
}
sizeofpm is 1. that is using count. to get the number of values in the array. How can i store values to that array? It doesnt seem to be working. Thanks!
you need to alloc it first. Try to change the first line to:
NSMutableArray* participants = [[NSMutableArray alloc] init];
also using setValue:forKey: wont work with an NSMutableArray as an array has no key.
Try using [participants addObject:pmpart_email];.
You don't create the array, you just declare it.
NSMutableArray *participants = [NSMutableArray array];
After that, setValue:forKey: will not add objects to an array. You need addObject::
[participants addObject:pmpart_email];
There is no key.
You are assigning a value to an NSMutableArray *participants like how you assign values to an NSDictionary object. To assign values to NSMutableArray you can call - (void)addObject:(id)anObject
So, I as some of the other answer have stated, you're missing your initializer for participants. However, judging by your use of setValue:forKey:, and how you appear to be structuring your data, you're not looking for NSMutableArray, but instead NSMutableDictionary. Arrays are simply lists, whereas dictionaries maintain key-value relationships, which you appear to be attempting to leverage.
Try this:
// some classes provide shorthand for `alloc/init`, such as `dictionary`
NSMutableDictionary *participants = [NSMutableDictionary dictionary];
for(int i = 0; i < sizeofpm; i++){
NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i];
NSString *pmpart_email = [pmpart_dict objectForKey:#"email"];
NSString *pmpart_email_extra = [#"pm" stringByAppendingString:pmpart_email];
[participants setValue:pmpart_email forKey:pmpart_email_extra];
NSLog(#"%#", participants);
}
This will give you a dictionary in the form of
{
pmpart_email_extra: pmpart_email
}

float* array to NSArray, iOS

I have a float pointer array and I would like to convert it to an NSArray.
Is there a better way to do it than to iterate through the float* and add each entry to the NSArray?
I have:
float* data = new float[elements];
fill up data from binary ifstream
I want to avoid doing something like:
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:elements];
for (int i=0;i<elements;i++)
{
[mutableArray addObject:[NSNumber numberWithFloat:data[i]]];
}
NSArray *array = [NSArray arrayWithArray:array];
Is there some convenience / more efficient method to copy a large chunk of floats into an NSArray?
Regards,
Owen
You’ve got two problems: first, you can’t store a float in an NSArray, since NSArrays will only hold Objective-C objects. You’ll need to wrap then in an object, probably NSNumber or NSValue.
As to your original question, since you have to create the objects anyway, there isn’t a better method. I’d recommend the for loop:
for (int i = 0; i < elements; i++) {
NSNumber *number = [NSNumber numberWithFloat:floatArray[i]];
[myArray addObject:number];
}
Keep in mind that number will be autoreleased. If you’re dealing with a lot of numbers, that can get out of hand pretty quickly with memory management, so you might do this instead:
for (int i = 0; i < elements; i++) {
NSNumber *number = [[NSNumber alloc] initWithFloat:floatArray[i]];
[myArray addObject:number];
[number release];
}