Custom Object empty after creation - ios

I have a custom object: Vendor that extends NSObject. I am initiating it like so:
NSDictionary *vendorObj = [vendors objectAtIndex:i];
Vendor *vendor = [[Vendor alloc] initWithVendorInfo:vendorObj];
NSLog(#"VendorObj: %#", vendorObj);
NSLog(#"Vendor: %#", vendor);
Here is what the class looks like:
#interface Vendor : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *description;
- (id)initWithVendorInfo:(NSDictionary *)vendorDetails;
#end
#implementation Vendor
- (id)initWithVendorInfo:(NSDictionary *)vendorDetails
{
self = [super init];
if(self)
{
_name = [vendorDetails[#"company_name"] copy];
_description = [vendorDetails[#"description"] copy];
}
return self;
}
If I NSLog vendorObj all the details are there. Once I initiate the Vendor object and NSLog it, the log shows:
2013-11-21 22:22:44.769 [48202:a07] Vendor:
I cannot seem to figure out why my object is nothing, no memory address, not even a null. What am I doing wrong here?

The problem is your description property. The NSObject class defines a description method. This method is called when you use a %# format specifier with an object.
Your description property is overriding that method.
Rename your description property to something else.

Related

Access and change values of an NSMutableDictionary from one class to another iOS

I am dealing with NSMutableDictionary in one class, But I want to change it's value in an another class.
Here is the code where it is declared.
#interface MyClass0 : NSObject
{
#public
NSMutableDictionary *valuee;
}
#property (nonatomic, copy) NSMutableDictionary *valuee;
#end
and in the implementation of myClass0 I do
#synthesize valuee;
and I also declare value as
valuee = #{#"name" : #"Aryan"};
Now I want to access and change the value of this dictionary in an another class.
Use #property (nonatomic, strong) NSMutableDictionary *valuee;
Now assign the value as
self.valuee = [[NSMutableDictionary alloc] init];
You can do this in the init method of your MyClass0.
Now you can access the value from another class like
MyClass0 *myClassInstance = [[MyClass0 alloc] init];
NSMutableDictionary *dict = myClassInstance.valuee;
You can write the getter by hand --
- valuee {
return valuee;
}
so the code instance.valuee will give you the original object not the copy.

Can't write NSDictionary parameters into custom Object

I was stuck on writing NSDictionary into Object process, I am sure that problem is simple as I imagine but would be great to get assistant. Here is my code:
my custom object:
#interface User : NSObject
#property (nonatomic, retain) NSString *cId;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *lastName;
....
-(instancetype) initWithParameters:(NSDictionary*) parameters;
#end
#import "User.h"
#implementation User
-(instancetype) initWithParameters:(NSDictionary*) parameters
{
self = [super init];
if (self) {
[self setParameters:parameters];
}
return self;
}
- (void) setParameters:(NSDictionary*) parameters{
_cId = parameters[#"cId"];
_firstName = parameters[#"first_name"];
_lastName = parameters[#"last_name"];
....
}
and writing process:
id userObjects = [resultData objectForKey:#"data"];
NSMutableArray* mUsers = [[NSMutableArray alloc] init];
for (NSDictionary* userParameters in userObjects) {
User *user = [[User alloc] initWithParameters:userParameters];
[mUsers addObject:user];
}
userObjects - NSArray got from JSON object from server data.
The problem is : nothing happening and user object still empty after initialization, then I have tried - setValuesForKeysWithDictionary after I called variables same as keys in dictionary and nothing changed.
after adding in mUsers:
Could anybody tell me what I am doing wrong? Thank you!
I believe you think those objects are uninitialized because you are seeing 0 key/value pairs next to each User object.
Your code looks good and I think things will change once you implement [NSObject description] (or [NSObject debugDescription]) like this:
- (NSString *)description
{
return [NSString stringWithFormat:#"cId=%#, firstName=%#, lastName=%#",
_cId, _firstName, _lastName];
}

How to print (using NSLog) all properties of a class automatically?

I think it's very difficult to print out the value of all properties of any class in objective-c, in the case the type of the property is complex.
But if the class that contains properties with simple types (like, NSString, int, double, boolean), is there any way to NSLog automatically instead of NSLog manually the value of each property?
Updated:
All the solutions you gave me are still manually. Is there any way like iterate through all properties of a class, and NSLog the variable_name and the variable_value. That's what I expected.
You can do this by overriding -(void)description method.
Example:
Let's say we have simple Car class.
#interface Car : NSObject
#property (copy, nonatomic) NSString *model;
#property (copy, nonatomic) NSString *make;
#property (strong, nonatomic) NSDate *registrationDate;
#property (assign, nonatomic) NSInteger mileage;
#property (assign, nonatomic) double fuelConsumption;
#end
#implementation
- (NSString*)description {
return [NSString stringWithFormat:#"<%#:%p %#>",
[self className],
self,
#{ #"model" : self.model,
#"make" : self.make,
#"registrationDate": self.registrationDate,
#"mileage" : #(self.mileage),
#"fuelConsumption" : #(self.fuelConsumption)
}];
}
#end
Putting this in NSDictionary will create very nice output in console.
On the other hand, you can create category on NSObject class and do something like this:
- (NSString*)myDescriptionMethod {
NSMutableDictionary *dict = [NSMutableDictionary new];
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
const char *property = property_getName(properties[i]);
NSString *propertyString = [NSString stringWithCString:property encoding:[NSString defaultCStringEncoding]];
id obj = [self valueForKey:propertyString];
[dict setValue:obj forKey:propertyString];
}
free(properties);
return [NSString stringWithFormat:#"<%# %p %#>",
[self class],
self,
dict];
}
Then you will avoid overriding -(void)description method in your classes.
Get it from here
The most elegant way to achieve what you're looking for in Objective-C with NSObject subclasses, it to override the NSObject method description.
For example (assuming your Class has a property called propertyX):
-(NSString *)description
{
return [NSString stringWithFormat:#"<myCustomObject: %#, propertyX: %f, %f>",
[self objectID], [self propertyX].x, [self propertyX].y];
}
The default description implementation of NSObject will simply return the memory address pointed to for the object, like so:
NSLog(#"%#", self);
2015-06-15 14:20:30.123 AppName[...] myCustomObject: 0x000000>
However, by overriding this base Class method as shown above, you will be able to customise this behavior, and the log will look like this:
2015-06-15 14:20:30.123 AppName[...] myCustomObject: 0x000000 someProperty, Property: blah, blah>
There is a nice tutorial, which discusses this further here.
Example :-
+ (NSString *)description;
[NSString description];
Gives you information about the class NSString.

RLMObject unable to use as a normal object

I have a Product model with the header:
#interface Product : RLMObject <NSCopying,NSCoding>
{
}
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *thumbnailURL;
#property (nonatomic, strong) UIImage *thumbnail;
-(id)initWithInfo:(NSDictionary*)dictionary;
-(UIImage*)getThumbnail;
and implementation:
#implementation Product
-(id)initWithInfo:(NSDictionary*)dictionary
{
self = [self init];
if (self) {
_title = dictionary[#"title"];
_thumbnailURL = dictionary[#"thumbnailURL"];
_thumbnail = [self getThumbnail];
}
return self;
}
-(UIImage*)getThumbnail
{
if (_thumbnail) {
return _thumbnail;
}
//load image from cache
return [self loadImageFromCache];
}
Now, when I try to create a Product object and insert it into Realm, I always get the exception
[RLMStandalone_Product getThumbnail]: unrecognized selector sent to instance 0xcd848f0'
Now, I remove _thumbnail = [self getThumbnail]; and it works fine. But then I get another exception
[RLMStandalone_Product title]: unrecognized selector sent to instance 0xd06d5f0'
when I reload my view. I have created my Product object in the main thread, so it should be fine to using its property and method, isn't it?
Any advice will be appreciated!
Because Realm object properties are backed by the database rather than in-memory ivars, accessing those properties' ivars is not supported. We're currently clarifying our docs to convey this:
Please note that you can only use an object on the thread from which is was created or obtained, ivars shouldn't be accessed directly for any persisted properties, and that getters and setters for persisted properties cannot be overridden.
So to work with Realm, your model should look like this:
#interface Product : RLMObject
#property NSString *title;
#property NSString *thumbnailURL;
#property (nonatomic, strong) UIImage *thumbnail;
#end
#implementation Product
-(UIImage*)thumbnail
{
if (!_thumbnail) {
_thumbnail = [self loadImageFromCache];
}
return _thumbnail;
}
-(UIImage*)loadImageFromCache
{
// Load image from cache
return nil;
}
+(NSArray*)ignoredProperties
{
// Must ignore thumbnail because Realm can't persist UIImage properties
return #[#"thumbnail"];
}
#end
And usage of this model could look like this:
[[RLMRealm defaultRealm] transactionWithBlock:^{
// createInDefaultRealmWithObject: will populate object keypaths from NSDictionary keys and values
// i.e. title and thumbnailURL
[Product createInDefaultRealmWithObject:#{#"title": #"a", #"thumbnailURL": #"http://example.com/image.jpg"}];
}];
NSLog(#"first product's image: %#", [(Product *)[[Product allObjects] firstObject] thumbnail]);
Notice how initWithInfo isn't necessary because RLMObject already has initWithObject: and createInDefaultRealmWithObject: already do this.

Program crush on [order setPrice:price] break point at #synthesize name

please help me to solve a simple problem.
I am a beginner in objective-c, and I am just switched to objective-c from java. I know java fair well, but not quite super deep into it.
I am building a iphone app. My app is quite simple.
The purpose of my iphone app is to take order with my iphone app in a restaurant.
Progress of My App:
My app only has couple viewPanels and buttons now :)
Here is my app sourcecode, firstview screenshot & secondview screenshot
Problem:
When i click on the Coffee button, my textField wont show up the coffee name & coffee price, which suppose to show up " coffee 1" .
and xcode will take me to the debugger from the iphone similator.(i think its crush at a line so the dubugger took me to the IBaction method and break at the line #synthesize name; It compiles with no error. please help trouble shoot why xcode take me to debugger when i press the coffee button.
SCREEN SHOWS UP RIGHT AFTER PRESS THE COFFEE BUTTON
here is the action code of the coffee button
- (IBAction)Coffee:(id)sender {
int price = 1;
NSString *name = #"coffee";
Storage *order = [[Storage alloc] init];
[order setName:name]; // i assume the program crush at here when it look into setName method.
[order setPrice:price];
[orders addOrders:order];
// Sanity check: // the program not even hit this line yet before crush;
NSLog(#"There are now %d orders in the array.", [orders.getOrders count]);
for (Storage *obj in orders.getOrders){
[check setText:[NSString stringWithFormat:#"%#",[obj description]]]; // check is the TextField instant varible. and the description method is from storage.m to print out the name and price.
}
}
The codes below are my storage classes that store all items that my customer orders.
it is a 2 dimensional array, and My Storages class is a wrapper class of Storage class.
the array format looks like this:
arrayindex1-> name, price
arrayindex2-> name, price
arrayindex3-> name, price
arrayindex4-> name, price
Storage.h
#import <Foundation/Foundation.h>
#interface Storage : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, assign) NSInteger price;
#end
Storage.m
#import "Storage.h"
#implementation Storage
#synthesize name; // program crush and goes to here.
#synthesize price;
- (NSString *)description {
// example: "coffee 1"
return [NSString stringWithFormat:#"%# %d", self.name, self.price];
}
#end
Storages.h
#import <Foundation/Foundation.h>
#import "Storage.h"
#interface Storages : NSObject
#property (nonatomic,strong, readwrite) NSMutableArray *orders;
-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;
#end
Storages.m
#import "Storages.h"
#implementation Storages
#synthesize orders;
- (id)init {
self = [super init];
if (self) {
orders = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addOrders:(Storage *)anOrder{
[orders addObject: anOrder];
}
-(NSMutableArray *) getOrders{
return orders;
}
#end
There are a couple of problems here.
1) Don't use a pointer for the price property. Generally, unless you're doing something unusual, your properties that are objects will be pointers and your properties that are primitives (NSInteger, BOOL, float, etc) will not be pointers.
2) You will want to make sure that the orders NSMutableArray is initialized with the Storages object, otherwise orders will remain nil and whenever you try to add objects to it, nothing will happen. To initialize the NSMutableArray, do this in your init method as shown below. You can also check that the object is actually getting into a valid mutable array this by putting a simple NSLog statement in the for (Storage *obj in orders.getOrders) { ... } loop and making sure you get at least one iteration through the loop. If orders.getOrders is nil, the work block of the for loop will never get run.
3) It sounds like you need to override (and may have already overridden) the -[NSObject description]method for your Storage object. My guess is you have a mismatch in this method with the -[NSString stringWithFormat:...] format string. For example, you might be using %d or %# in the format string for the NSInteger *. Something like that could definitely cause a crash (which is what I think you mean by "Xcode taking you to the debugger"). For NSIntegers you need to use %d or %i. And as myself and others have mentioned, you want NSInteger here and not NSInteger * and you should change your property declaration.
4) Based on what you have here, I don't think you need the order property in the Storages class at all.
5) Make sure you haven't overlooked the possibility of forgetting to hook up the IBOutlet in Interface Builder to the check textField. A good test for this, besides just confirming it's connected in Interface Builder, would be a reality check test like [check setText:#"This is a test."];
6) Keep in mind that once this works, your for loop is going to execute very quickly, and you'll immediately see only the description for the last object in the orders array. But that doesn't seem to be what your question is about.
I'd suggest you make the following changes:
Storage.h
#import <Foundation/Foundation.h>
#interface Storage : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, assign) NSInteger price;
#end
Storage.m
#import "Storage.h"
#implementation Storage
#synthesize name;
#synthesize price;
- (NSString *)description {
// example: "coffee 1"
return [NSString stringWithFormat:#"%# %d", self.name, self.price];
}
#end
Your IBAction method
- (IBAction)Coffee:(id)sender {
int price = 1;
NSString *name = #"coffee";
Storage *order = [[Storage alloc] init];
[order setName:name];
[order setPrice:price];
[orders addOrders:order];
// Sanity check:
NSLog(#"There are now %d orders in the array.", [orders.getOrders count]);
for (Storage *obj in orders.getOrders){
[check setText:[NSString stringWithFormat:#"%#",[obj description]]]; // check is the TextField instant varible. and the description method is from storage.m to print out the name and price.
}
}
Storages.h
#import <Foundation/Foundation.h>
#import "Storage.h"
#interface Storages : NSObject
#property (nonatomic,strong, readwrite) NSMutableArray *orders;
-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;
#end
Storages.m
#import "Storages.h"
#implementation Storages
#synthesize orders;
- (id)init {
self = [super init];
if (self) {
orders = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addOrders:(Storage *)anOrder{
[orders addObject: anOrder];
}
-(NSMutableArray *) getOrders{
return orders;
}
#end
What does description do in the following? (I cannot see any description object in Storage class):
[check setText:[NSString stringWithFormat:#"%#",[obj description]]];
I think if you want to print the name the do like:
[check setText:[NSString stringWithFormat:#"%#: %#",obj.name, obj.price]];
You have taken NSInteger pointer in Storages class which is not correct. NSInteger is basic data type and not a pointer. Remove that pointer and use NSInteger variable.
I hope this would resolve your problem.
You could use below code:
#interface Storage : NSObject
#property (nonatomic, retain)NSString *name;
#property (nonatomic, assign)NSInteger price;
You have two mistakes:
1- You declared price as an NSInteger and passed it as a reference. The correct is to pass it as an integer as it is and deal with it as an integer through the whole application.
2- You didn't initialize orders array in Storages class so it will be always nil and will not hold any added object.
You code may looks like:
In the button's IBAction pass the price directly
[order setPrice:price];
In the Storage class
- (NSString *)description {
return [NSString stringWithFormat: #"%# %d", name, price];
}
Add the following to the Storages class
-(id)init
{
if (self = [super init])
{
orders = [[NSMutableArray alloc] init];
}
return self;
}

Resources