Properties of objects on array (Objective-c) - iphone

I have a NSObject class which's name is test.
class test has 3 property. Name, age, id;
I have 3 Objects in test class. s, b, c.
I am putting all of the objects to the array with: NSArray *ary = [NSArray arrayWithObjects:#"a", #"b", #"c", nil];
I am trying to access to the data of property in that array. Which means I have to read, write the property of the object in array in the loop (for loop or while loop).
I found a lot of materials on the internet. The method that I was close to do was:
[[ary objectAtIndex:0] setName:#"example"];
This method was working with setters and getters. But it did give a horrible error. Is there any "WORKING" method to do it?
Thanks...

Let's imagine a Person class:
#interface Person : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic) NSInteger age;
#property (nonatomic) long long identifier;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age identifier:(long long)identifier;
#end
#implementation Person
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age identifier:(long long)identifier {
Person *person = [[self alloc] init];
person.name = name;
person.age = age;
person.identifier = identifier;
return person;
}
#end
You can then create an array of people like so:
NSArray *people = #[[Person personWithName:#"Rob" age:32 identifier:2452323],
[Person personWithName:#"Rachel" age:29 identifier:84583435],
[Person personWithName:#"Charlie" age:4 identifier:389433]];
You can then extract an array of people's names like so:
NSArray *names = [people valueForKey:#"name"];
NSLog(#"%#", names);
That will generate:
2013-09-27 14:57:13.791 MyApp[33198:a0b] (
Rob,
Rachel,
Charlie
)
If you want to extract information about the second Person, that would be:
Person *person = people[1];
NSString *name = person.name;
NSInteger age = person.age;
long long identifier = person.identifier;
If you want to change the age of the third person, it would be:
Person *person = people[2];
person.age = 5;
Or, if you want to iterate through the array to extract the information, you can do that, too:
for (Person *person in people) {
NSString *name = person.name;
NSInteger age = person.age;
long long identifier = person.identifier;
// now do whatever you want with name, age, and identifier
}

Try this
STEP 1 : Cast it to the appropriate object type first
s *myS = (s *)[array objectAtIndex:0];
b *myB = (b *)[array objectAtIndex:1];
c *myC = (c *)[array objectAtIndex:2];
STEP 2 : Set / get whatever property you want to
myS.name = #"example";

Related

Converting ManagedObject to NSObject

I am trying to get the data from 'Employee' Entity which has empId, Name, deptId as attributes.
// Used to Populate data in table
Employee : NSObject
#property (nonatomic, retain) NSString *name;
#property int empId;
#property int deptId;
#synthesize empId, name, deptId;
&
CDEmployee : NSManagedObject
#property (nonatomic, retain) NSString *name;
#property int empId;
#property int deptId;
- (void)convertMyData:(Employee *)emp;
#dynamic empId, name, deptId;
- (void)convertMyData:(Employee *)emp
{
self.empId = emp.empId;
self.name = emp.name;
self.deptId = emp.deptId;
}
// My code to fetch & convert data retured from db to Employee class
-(NSArray *)getAllEmployees:(NSManagedObjectContext*)context
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(empId > %#)",[NSNumber numberWithInt:-1],[NSNumber numberWithInt:1]];
NSFetchRequest* req = [self createRequest:context]; // Request is correct
[req setPredicate:predicate];
NSFetchedResultsController* fetchContr = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
NSError* error = nil;
[fetchContr performFetch:&error];
NSArray *fetchedObjects = [fetchContr fetchedObjects];
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
for(int i = 0; i < [fetchedObjects count]; i++)
{
CDEmployee *cdEmp = [[fetchedObjects objectAtIndex:i] retain]; // just tried retaing
if (cdEmp)
{
Employee *emp = [[Employee alloc] init];
NSLog(#"Employee - %#", emp); // Shows tht is has object
[cdEmp convertMyData:emp]; //-----> Crashing here
[result addObject:emp];
[emp release];
}
}
[fetchContr release];
return result;
}
I am fetching results from core data & I am getting correct results back, but when I convert my core data result back to Employee(NSObject) class i am getting [NSManagedObject convertMyData:]: unrecognized selector sent to instance.
I tried adding another method say -(void)helloWorld to CDEmployee class & tried [cdEmp helloWorld]; but got the same crash.
Not getting why its causing the problem. I have method defined & implemented at proper place & its not even giving warning to me at compile time.
Here is a sample code that converts NSManagedObject to NSDictionary.
NSDictionary is even more easier to handle and operate on the data than NSObject. Also refer this Git Library for other similar iOS utility class methods.
+(NSDictionary *)convertManagedObjectToDictionary:(NSManagedObject *)managedObject{
if (!managedObject){
NSLog(#"Managed object is nil");
return nil;
}
else{
unsigned int objectsCount;
objc_property_t *objectProperties = class_copyPropertyList([managedObject class], &objectsCount);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for(int i = 0; i < objectsCount; i++) {
objc_property_t property = objectProperties[i];
NSString *name = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
id object = [managedObject valueForKey:name];
[dictionary setObject:((object == nil) ? [NSNull null] : object) forKey:name];
}
free(objectProperties);
return dictionary;
}
}
I tried in different way & got resolved http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
First, you should really first think about what you want to accomplish from a non-technical perspective. What is it, actually?
Second, NSManagedObject is a subclass of NSObject, which means you already have a NSObject. Congratulations, you are done!
Also, in your code you call fetchMyData but did not show us where you define it or what the code looks like. Maybe a NSManagedObject category would be in order here? But still, this begs the question why you would want to do this. Even if you were to convert into a Foundation object, such as NSDictionary as suggested in the other answer, you would still have a problem with modelling relationships...
Finally, what SDK are you using? You can't be serious if you are still not using ARC.

How to show two objects in one cell (tableView)?

I ran into a small problem. I created a UITableView where the user can add his contacts from his addressbook. The firstName gets displayed in the first row, the lastName in the second one. I know where the problem is, because right now I actually tell him how to do that.
But how can I tell him to display both names in the same cell? I did it with the following methods but they don't work.
-(void)addObjectsFromArray:(NSArray *)otherArray
{
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObjectsFromArray:menuArray];
NSLog(#"%#", myArray);
[self.tableView reloadData];
}
The problem is that I cannot display objects from myArray because cellforRowAtIndex method does not get the value. And with that one I get the same result (firstName first Row, lastName second Row):
NSArray *objects = [[NSArray alloc] initWithObjects:firstName, lastName, nil];
NSString *cellValue = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
NSLog(#"%#", objects);
return cell;
Not only does your code display the first name and last name in different cells, it also does not work for rows at indexes greater than one. This is because your table data source reads from a brand-new array that you create with alloc/init on the first line, rather than reading from your model. Your little array contains only two objects, explaining the two rows.
Here is what you need to do: first, make an array of users available to your table's data source. Suppose it's called userArray. Put user data for each user into that array. Suppose each user is stored as an object of type MYUser that responds to [user firstName] and [user lastName] calls with the first and the last name of the user.
Now put this code into your table's data source:
MYUser *user = [userArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:#"%# %#", [user firstName], [user lastName]];
cell.textLabel.text = cellValue;
EDIT : If your menuArray stores the first and the last names in separate consecutive string objects, you can still do it (though I recommend against it, because it will be confusing to people who maintain your program in the future).
First, you need to change the method that returns the number of items in the table to return menuArray.count / 2 instead of just menuArray.count. Then you can modify your code as follows:
NSString firstName = [menuArray objectAtIndex:indexPath.row/2];
NSString lastName = [menuArray objectAtIndex:indexPath.row/2 + 1];
NSString *cellValue = [NSString stringWithFormat:#"%# %#", firstName, lastName];
cell.textLabel.text = cellValue;
EDIT2 : Here is how you create a class that stores the first and the last name of a user. You add the #interface part to MYUser.h, and the #implementation part to MYUser.m file. You then import MYUser.h in .m files from which you reference MYUser class, and use initWithFirstName:andLastName: to initialize new instances. The sample below assumes that you use ARC:
MYUser.h file:
#import <UIKit/UIKit.h>
#interface MYUser : NSObject
#property (nonatomic, strong) NSString *firstName;
#property (nonatomic, strong) NSString *lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last;
#end
MYUser.m file:
#implementation MYUser
#synthesize firstName, lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last {
self = [super init];
if (self) {
self.firstName = first;
self.lastName = last;
}
return self;
}
#end

How to create Array of Array in iPhone?

I want to create a nested array or multidimensional array.
In my data is,
FirstName class year dept lastName
Bob MBA 2000 Comp Smith
Jack MS 2001 Comp McDonald
NSMutableArray *section = [[NSMutableArray alloc] init];
I want to put my data into the section Array.
Eg:
section[0] = [FirstName,LastName];
section[1] = [class, year, dept];
So how can i put the values into array like that.
Please help me out.
Thanks
I would recommend creating a custom data storage class. You could call it PDPerson.h You'll also need the .m file. For each property, do something like this:
In the .h: Declare each of your properties like so:
#interface PDPerson : NSObject{
}
#property(nonatomic, retain) NSString *firstName;
#property(nonatomic, retain) NSString *lastName;
#property(nonatomic, retain) NSString *class;//May want to consider renaming
#property(nonatomic, retain) NSString *year;
#property(nonatomic, retain) NSString *dept;
#end
Then in the .m:
#implementation
#synthesize firstName, lastName;
#synthesize class, year dept;
-(void)dealloc{
[firstName release];
[lastName release];
[class release];
[year release];
[dept release];
}
Each time you want to create a new "Person" in your array, do this:
PDPerson *person = [[PDPerson alloc]init];
You can then easily set the properties of the object like so:
person.firstName = #"John";
person.lastName = #"Smith";
person.class = #"Math";
person.year = #"1995";
person.dept = #"Sciences";
And retrieve them:
firstNameLabel.text = person.firstName;
The nice thing about these objects is that all you have to do now is add the person object to your array:
NSMutableArray *personArray = [[NSMutableArray alloc] init];
[personArray addObject:person];
NSArray *section1 = [NSArray arrayWithObjects: #"1,1", #"1,2", #"1,3", nil];
NSArray *section2 = [NSArray arrayWithObjects: #"2,1", #"2,2", #"2,3", nil];
NSArray *section3 = [NSArray arrayWithObjects: #"3,1", #"3,2", #"3,3", nil];
NSArray *sections = [NSArray arrayWithObjects: section1, section2, section3, nil];
int sectionIndex = 1;
int columnIndex = 0;
id value = [[sections objectAtIndex:sectionIndex] objectAtIndex:columnIndex];
NSLog(#"%#", value); //prints "2,1"
Be warned, this isn't a flexible way of storing data. Consider using CoreData or creating your own classes to represent the data.
You can just nest multiple NSArray instances within an NSArray.
For example:
NSMutableArray* sections = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfSections; i++)
{
NSMutableArray* personsInSection = [[NSMutableArray alloc] init];
[sections insertObject:personsInSection atIndex:i];
for (int x = 0; x < numberOfPersons; x++)
{
Person* person = [[Person alloc] init];
[personsInSection insertObject:person atIndex:x];
}
}
This may seem like overkill when coming from languages such as C++ or Java, where multidimensional arrays can be created simply by using multiple sequare brackets. But this is way things are done with Objective-C and Cocoa.

Sorting NSDictionary with multiple constraints

I have a NSDictionary collection whose key is a unique id and value is an array with two different objects (FruitClass, ProductClass) and I would like to group the collection such that it's sorted first by ProductClass.productName and then by FruitClass.itemName.
So the final list would look something like:
{apple, butter}
{apple, pie}
{banana, daiquiri}
{banana, smoothie}
{melon, zinger}
where the first item is a FruitClass instance item and second is a ProductClass instance item.
What's the best way to go about doing this? Most of the examples I've come across are done on one key. How do you do it with an NSDictionary that has 2 different object types?
Looking at NSDictionary's's keysSortedByValueUsingSelector,
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
I get the impression that you would create the 'compare' method on the class type of the value object. So for multiple field sort, would I have to resort to creating a new object type, 'CombinedClass' which contains FruitClass & ProductClass and implement a 'compare' to make this happen?
FruitClass:
{
NSString *itemName;
}
#end
#interface ProductClass
{
NSString *productName;
}
#end
If there is a data structure that consists of only one fruit and only one product then an array is not really a good option. You can use another class and provide a compare: comparator:
#interface ComboClass : NSObject
{
FruitClass *fruit;
ProductClass *product;
}
#property(nonatomic,retain) FruitClass *fruit;
#property(nonatomic,retain) ProductClass *product;
- initWithFruit:(FruitClass *)f andProduct:(ProductClass *) p;
#end
#implementation ComboClass
#synthesize fruit;
#synthesize product;
- (void) dealloc
{
[fruit release];
[product release];
[super dealloc];
}
- initWithFruit:(FruitClass *)f andProduct:(ProductClass *) p
{
self = [super init];
if (!self) return nil;
self.fruit = f; // some recommend against accessor usage in -init methods
self.product = p;
return self;
}
- (NSComparisonResult) compare:(id) another
{
NSComparisonResult result = [self.fruit.itemName compare:another.fruit.itemName];
if (result == NSOrderedSame)
return [self.product.productName compare:another.product.productName];
else
return result;
}
#end
Alternatively, you might be able to use an NSDictionary with product and fruit key-value pairs, (so you'll end up with dictionaries inside a dictionary). The NSSortDescriptor class can be used to sort arrays using values of key-paths, so it might be another option to explore.
You could add a category to NSArray that would do the comparison and you wouldn't have to create another class.
#interface NSArray ( MySortCategory )
- (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare;
#end
The implementation should be pretty straightforward based on your description.
Edit I got a little miffed that this was marked down with no comment, so I did a full implementation to make sure it would work. This is a little different than your sample, but the same idea.
FruitsAndProducts.h
#interface Fruit : NSObject
{
NSString *itemName;
}
#property(nonatomic, copy)NSString *itemName;
#end
#interface Product : NSObject
{
NSString *productName;
}
#property(nonatomic, copy)NSString *productName;
#end
FruitsAndProducts.m
#import "FruitsAndProducts.h"
#implementation Fruit
#synthesize itemName;
#end
#implementation Product
#synthesize productName;
#end
NSArray+MyCustomSort.h
#interface NSArray (MyCustomSort)
- (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare;
#end
NSArray+MyCustomSort.m
#import "NSArray+MyCustomSort.h"
#import "FruitsAndProducts.h"
#implementation NSArray (MyCustomSort)
- (NSComparisonResult)customCompareToArray:(NSArray *)arrayToCompare
{
// This sorts by product first, then fruit.
Product *myProduct = [self objectAtIndex:0];
Product *productToCompare = [arrayToCompare objectAtIndex:0];
NSComparisonResult result = [myProduct.productName caseInsensitiveCompare:productToCompare.productName];
if (result != NSOrderedSame) {
return result;
}
Fruit *myFruit = [self objectAtIndex:1];
Fruit *fruitToCompare = [arrayToCompare objectAtIndex:1];
return [myFruit.itemName caseInsensitiveCompare:fruitToCompare.itemName];
}
#end
Here it is in action
// Create some fruit.
Fruit *apple = [[[Fruit alloc] init] autorelease];
apple.itemName = #"apple";
Fruit *banana = [[[Fruit alloc] init] autorelease];
banana.itemName = #"banana";
Fruit *melon = [[[Fruit alloc] init] autorelease];
melon.itemName = #"melon";
// Create some products
Product *butter = [[[Product alloc] init] autorelease];
butter.productName = #"butter";
Product *pie = [[[Product alloc] init] autorelease];
pie.productName = #"pie";
Product *zinger = [[[Product alloc] init] autorelease];
zinger.productName = #"zinger";
// create the dictionary. The array has the product first, then the fruit.
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:zinger, banana, nil], #"zinger banana", [NSArray arrayWithObjects:butter, apple, nil], #"butter apple", [NSArray arrayWithObjects:pie, melon, nil], #"pie melon", nil];
NSArray *sortedKeys = [myDict keysSortedByValueUsingSelector:#selector(customCompareToArray:)];
for (id key in sortedKeys) {
NSLog(#"key: %#", key);
}
Your comparator can work on whatever you throw at it... you can make it treat its two arguments as NSArray objects, if this is what you need. When you put arrays as values into your dictionary, then just use those - no need for another class.
If you want to build a new class anyway (maybe for design reasons) - go for it, but it is not a "must do" here.
Edit: strike out to make clear that only one argument is given - as the other one is the object the selector is called on. Using NSArray will need a class extension, a custom class is much cleaner.

Filling class variables from an NSDictionary in a loop

I would like to fill in the class variables in a loop from an dictionary. What I want to do is having the dictionary key as a class variable and assign the class variable (the dictionary key) the value from dictionary... something like this:
+(void) initWithDictionary:(NSDictionary *)dic {
MyClass *classInstance = [[[self alloc] init] autorelease];
NSArray *allKeys = [dic allKeys];
for(NSUInteger i = 0; i < [allKeys count]; i++)
{
id classVariable = [allKeys objectAtIndex:i];
classInstance.classVariable = [dic objectForKey:[allKeys objectAtIndex:i]];
}
return classInstance;
}
It does not work, because I do not know how to assign the class variable from the string.
Thanks for answer, I am returning a JSON string that gives me an NSDictionary with keys and values. I am trying to fill this values to my class, let's say DetailObject. I want to use later in the project the DetailObject.id, DetailObject.description, etc. I would like to do it in a loop, becouse now I have to write this:
+ (id) initWithDiccionary :(NSDictionary *)dic//;
{
//Instantiating an object of this class... that's okay.
DetailObject *classInstance = [[[self alloc] init] autorelease];
classInstance.id = [dic objectForKey#"id"];
classInstance.desc = [dic objectForKey#"desc"];
etc... etc...
return classInstance;
}
What I want is to parse the dictionary from JSON to my object and respective variables and values that comes from dictionary in a loop, because if the JSON dictionary changes, I just add the new class variable with the same name of the returned dictionary key...
I do not know if I have explained it well...
Your question is very very unclear and I have no idea what you're trying to do or why. But just looking at your code I can tell you already that it's definitely not doing what you want.
//There should be no semicolon after "dic" below.
//Also, you should be returning a MyClass or an id.
- (id) initWithDiccionary :(NSDictionary *)dic//;
{
//Instantiating an object of this class... that's okay.
MyClass *classInstance = [[[self alloc] init] autorelease];
//Getting all the keys from the dictionary, seems fine...
NSArray *allKeys = [dic allKeys];
//Looping through all the keys in the dictionary, seems okay...
for(NSUInteger i = 0; i < [allKeys count]; i++)
{
//Storing the current key.
id classVariable = [allKeys objectAtIndex:i];
//Assigning the class's property "classVariable" to match the current key's value.
//No reason to say "[allKeys objectAtIndex:i]" again, though.
classInstance.classVariable = [dic objectForKey:classVariable];
}
//Returning something when you have no return type above (void) is wrong.
return classInstance;
}
Your code will just assign classInstance.classVariable to be equal to [allKeys objectAtIndex:[allKeys count]-1]. Your loop is pointless.
After I actually annotated your code though I think I have some idea of what you want. Basically you want to assign the variables with names matching the keys in the dictionary the values in the dictionary. i.e. if there is a key called "superKey" then you want to find the variable within classInstance (classInstance.superKey) and assign it the value in the dictionary that matches superKey. That's what you want, right?
Well, the only way I know of to do that is to use a big switch statement or a bunch of if statements. Make some function within MyClass like this:
- (void) assignProperty:(id)property toValue:(id)value
{
if (property == #"superKey")
{
self.superKey = value;
}
else if (property == #"lameKey")
{
self.lameKey = value;
}
//etc.
}
Then you just call [classInstance assignProperty:classVariable toValue:[doc objectForKey:classVariable]] and the job will be done.
But having told you all that...
Why would you ever want to do what you're doing? Want to know a much better way of doing this? Give MyClass its own NSDictionary. Basically all you are doing is defeating the entire purpose of the dictionary. Why? They are incredibly fast to access and can store whatever you want. There is no reason not to use one. So just do this:
- (id) initWithDiccionary :(NSDictionary *)dic
{
MyClass *classInstance = [[[self alloc] init] autorelease];
classInstance.dictionary = dic;
return classInstance;
}
Voila.
Enter Key-Value Coding. The following is an example of how you could achieve your desired outcome:
#interface MyClass : NSObject
#property (nonatomic, retain) NSString *aString;
#property (nonatomic, retain) NSNumber *aNumber;
#property (nonatomic, retain) NSString *yetAnother;
- (id)initWithDictionary:(NSDictionary *)dictionary;
#end
#implementation MyClass
#synthesize aString;
#synthesize aNumber;
#synthesize yetAnother;
- (id)initWithDictionary:(NSDictionary *)dictionary {
if ((self = [super init])) {
[self setValuesForKeysWithDictionary:dictionary];
}
return self;
}
// dealloc is left as an exercise for the reader
#end
You could use this class as follows:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"my string", #"aString",
[NSNumber numberWithInt:42], #"aNumber",
#"strings!", #"yetAnother", nil];
MyClass *myClass = [[MyClass alloc] initWithDictionary:dictionary] autorelease];
// yay!
You can thank Objective-C's dynamism for that. :)