I get an error when trying to access an attribute of a class "event"
Json List all Ok When I trying to access the error jumps
Evento* bean = [ListaEventos objectAtIndex:indexPath.row];
DetalleViewController *detalle = [self.storyboard instantiateViewControllerWithIdentifier:#"Detalle"];
NSLog(#"detalle: %#",[bean nombre]);//bean Log OK!!, but bean.nombre Error!! why?
The class: evento.h
#interface Evento : NSObject
#property (strong, nonatomic) NSString *idevento;
#property (strong, nonatomic) NSString *nombre;
#property (strong, nonatomic) NSString *descripcion;
#property (strong, nonatomic) NSString *fecha;
#property (strong, nonatomic) NSString *telefono;
#property (strong, nonatomic) NSString *direccion;
#end
Evento.m
#implementation Evento
#synthesize idevento;
#synthesize nombre;
#synthesize descripcion;
#synthesize fecha;
#synthesize telefono;
#synthesize direccion;
#end
What you get out of [ListaEventos objectAtIndex:indexPath.row]; is of kind NSDictionary and not of kind Evento as supposed. Check your data!
The object you get with
[ListaEventos objectAtIndex:indexPath.row];
Is of type dictionary which doesn't have the method or the property nombre.
If you could add also add here the json we can help you to get the correct evento object.
Related
I have a singleton class which I intend to share throughout my whole application.
It looks like this:
.m
#implementation PersonalGlobal
#synthesize firstName;
#synthesize lastName;
#synthesize SSN;
#synthesize customerNo;
#synthesize email;
#synthesize address;
#synthesize city;
#synthesize postalCode;
#synthesize telNo;
#synthesize mobileNo;
#pragma mark Singleton Methods
+ (id)sharedPersonal {
static PersonalGlobal *sharedPersonalGlobal = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedPersonalGlobal = [[self alloc] init];
});
return sharedPersonalGlobal;
}
- (void)dealloc {
[super dealloc];
// Should never be called
}
#end
.h
#import <foundation/Foundation.h>
#interface PersonalGlobal : NSObject {
NSString *firstName;
NSString *lastName;
NSString *SSN;
NSString *customerNo;
NSString *email;
NSString *address;
NSString *city;
NSString *postalCode;
NSString *telNo;
NSString *mobileNo;
}
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *lastName;
#property (nonatomic, retain) NSString *SSN;
#property (nonatomic, retain) NSString *customerNo;
#property (nonatomic, retain) NSString *email;
#property (nonatomic, retain) NSString *address;
#property (nonatomic, retain) NSString *city;
#property (nonatomic, retain) NSString *postalCode;
#property (nonatomic, retain) NSString *telNo;
#property (nonatomic, retain) NSString *mobileNo;
+ (id)sharedPersonal;
#end
In the code I save strings to it like so:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
sharedPersonal.firstName = #"Some string";
But when I change view and try to access the string like this:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
//Setting some textfield
sometextfield.text = sharedPersonal.firstName;
I get nothing. I have done a #import "PersonalGlobal.h" in all the files.
Can i commit the changes in any way to the singleton class?
Can anyone see what I am doing wrong here?
Thanks!
Your implementation is looking fine. This should work if your singleton returning the only one instance . So the point of doubt in your sharedPersonal method . Just try to add breakpoints in this method and see whether it is creating a new instance every time .for the reference I got this SO question.
If not then you can also try this :
+(SingleTon *)getSharedInstance
{
static PersonalGlobal *sharedPersonalGlobal = nil;
if (sharedPersonalGlobal==nil)
{
sharedPersonalGlobal=[[PersonalGlobal alloc]init];
}
return sharedPersonalGlobal;
}
This is working code for me.
Your singleton implementation looks ok. I wouldn't be surprised if in this case the code that you wrote to set the firstName just never gets executed. If I were you I would step through the codes.
In my .h file I have this:
#protocol ScanBookDelegate
- (void)MethodNameToCallBack:(NSArray *)s;
#end
#property (nonatomic, weak) id <ScanBookDelegate> delegate;
In my .m file I have:
#synthesize delegate;
I get this error:
Existing ivar 'delegate' for __weak property 'delegate' must be __weak
How do I resolve this error?
I am returning an NSArray * as I am returning data from a JSON result.
UPDATE:
Here is my entire .h file -
#import <UIKit/UIKit.h>
#protocol ScanBookDelegate
- (void)MethodNameToCallBack:(NSArray *)s;
#end
#interface jabBookScan : NSObject
<NSURLConnectionDelegate, NSURLConnectionDataDelegate>
{
NSURLConnection *internalConnection;
NSMutableData *container;
id <ScanBookDelegate> delegate;
}
- (id)initWithRequest:(NSURLRequest *)req;
- (void)start;
- (NSMutableData *) lookupBook:(NSString *) isbn;
- (void)fetchedData:(NSData *)responseData;
#property (nonatomic, copy) NSURLRequest *request;
#property (nonatomic, copy) void (^completionBlock)(id obj, NSError *err);
#property (nonatomic, strong) NSDictionary *jsonRootObject;
#property NSMutableData *responseData;
#property NSURL *myURL;
#property (nonatomic, weak) id <ScanBookDelegate> delegate;
#end
Get rid of id <ScanBookDelegate> delegate; in your {} (at the top) and it should work. You don't need to specify that when using properties. If you really want to, then you can declare that ivar __weak so it matches your property (ivars are strong by default).
I have been working on an app all day which has been working fine until Xcode downloaded libraries or something and then it started having issues. I am just trying to create the getter/setter methods to get a couple of arrays out of my APPDelegate. Like I said it was working fine and then randomly it showed up with this error and now won't build anymore:
property with 'retain(or strong)' attribute must be of object type
Here is the rest of the code:
#import <Foundation/Foundation.h>
#import "Project.h"
#import "TeamMember.h"
#interface Task : NSObject{
NSDate *endDate;
NSDate *startDate;
NSMutableString* notes;
NSMutableString* taskName;
//The error appears first right here over teamMember
TeamMember *teamMember;
Project *project;
}
//The error appears over both of the following lines as well...
#property (nonatomic, retain)TeamMember *teamMember;
#property (nonatomic, retain) Project * project;
#property (nonatomic, retain) NSMutableString *notes;
#property (nonatomic, retain) NSMutableString *taskName;
#property (nonatomic, retain) NSDate *startDate;
#property (nonatomic, retain) NSDate *endDate;
#end
Any ideas? This has got me stumped....
Here is Project.h:
#import <Foundation/Foundation.h>
#import "Task.h"
#interface Project : NSObject{
NSDate *dueDate;
NSDate *startDate;
NSArray *tasksInProject;
NSMutableString* notes;
NSMutableString* description;
NSMutableString* projectName;
}
#property (nonatomic, retain) NSDate *startDate;
#property (nonatomic, retain) NSDate *dueDate;
#property (nonatomic, retain) NSArray *tasksInProject;
#property (nonatomic, retain) NSMutableString *description;
#property (nonatomic, retain) NSMutableString *projectName;
#end
Here is TeamMember.h
#import <Foundation/Foundation.h>
#import "Task.h"
#import "Project.h"
#interface TeamMember : NSObject{
NSMutableArray *projects;
NSMutableString *name;
NSMutableString *title;
NSMutableString *email;
NSMutableString *phone;
NSMutableString *notes;
}
//#property(nonatomic, retain) NSArray *projects;
#property (nonatomic, retain) NSMutableString *name;
#property (nonatomic, retain) NSMutableString *title;
#property (nonatomic, retain) NSMutableString *email;
#property (nonatomic, retain) NSMutableString *phone;
#property (nonatomic, retain) NSMutableString *notes;
#end
It looks like it's caused by recursively including of header files.
Try to add #class Project and #class TeamMember into your Task.h, like this
#import <Foundation/Foundation.h>
#import "Project.h"
#import "TeamMember.h"
#class TeamMember;
#class Project;
#interface Task : NSObject{
NSDate *endDate;
NSDate *startDate;
...
}
#end
You are attempting to retain something that is not an NSObject subclass. Usually this happens when someone tries to retain a float or int.
NSInteger is a scalar and not an object. So you shouldn't retain it, it should be assigned. Changing your property will clear up the warning message. You don't need to do the NSNumber stuff that you added in.
#property (nonatomic, assign) NSInteger integValue;
The error is that you are attempting to retain something that is not an NSObject subclass. Usually this happens when someone tries to retain a float or int.
You've shown the .h for Project but not for TeamMember. Check the latter for this, and if you don't see it then update your code snippet.
It happened to me because i have recursively imported some .h files.
I removed those files and my project started working.
Answer is already given But in my case the issue was different.
I resolved it in a different way. In .h file Foundation framework is added by default. So i commented the line and import UIKit framework. Now i am able to run my code.
//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
In case we custom a View, to use IBOutlet in ViewController
we simply import header of custom view like this in our ViewController
#import "abc.h"
this works for sure
Don't Write import statement .
Write #class.
Example :-
#class SecondViewController.
Thanks
I want to read the value from my DatePicker and store it in my singleton, but I get an exception when trying to do this.
Here is the singleton interface:
#interface Helper : NSObject {
NSMutableArray *array;
//Information For Filter page
NSMutableArray *towns;
NSMutableArray *types;
//Shared resources to apply filters
NSDate *toDate;
NSDate *fromDate;
NSString *selectedType;
NSString *selectedTown;
//Shared resource of which button was clicked
int clicked;
int clickedCell;
int clickedContext;
}
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic, retain) NSMutableArray *towns;
#property (nonatomic, retain) NSMutableArray *types;
#property (nonatomic) int clicked;
#property (nonatomic) int clickedCell;
#property (nonatomic) int clickedContext;
#property (nonatomic, retain) NSDate *toDate;
#property (nonatomic, retain) NSDate *fromDate;
#property (nonatomic, retain) NSString *selectedType;
#property (nonatomic, retain) NSString *selectedTown;
+(id)sharedManager;
#end
This is the function where the exception happens
-(void) viewWillDisappear:(BOOL)animated
{
Helper *myHelper = [Helper sharedManager];
if(myHelper.clickedContext == 0)
{
if(myHelper.clickedCell == 0)
{
//causes exception
myHelper.fromDate = [self.fromDatePicker date];
}
else
{
//causes exception
myHelper.toDate = [self.toDatePicker date];
}
}
else
{
if(myHelper.clickedCell == 0)
{
myHelper.selectedTown = [myHelper.towns objectAtIndex:[self.townPicker selectedRowInComponent:0]];
}
else
{
myHelper.selectedType = [myHelper.types objectAtIndex:[self.typePicker selectedRowInComponent:0]];
}
}
[super viewWillDisappear:animated];
}
declaration of pickers
#property (nonatomic, retain) IBOutlet UIDatePicker *toDatePicker;
#property (nonatomic, retain) IBOutlet UIDatePicker *fromDatePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *typePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *townPicker;
#synthesize part
#synthesize typePicker, toDatePicker, fromDatePicker, townPicker, townsView, toDateView, typesView, fromDateView;
Any idea's why?
Thanks
If your outlet is not assigned in interface builder this can happen.
If you think it is, try running the app with NSZombieEnabled = YES and see if you get any error messages.
I found the problem. In my Helper Class I should have assigned not retained. So it should have been
#property (nonatomic, assign) NSDate *toDate;
#property (nonatomic, assign) NSDate *fromDate;
I'm getting this same error, but I've checked to make sure the properties were set correctly in the .h file.
Here's the code:
NSUInteger theSizesCount = [theWho.theSizes count];
The error is "error: request for member theSizes in something not a strucutre or union. This .m file is importing 6 .h files, and 4 of them have the same properties in theWho, but it's related to various Super Classes. This .m file is implementing only one of them, and theWho and theSize are sythesized.
Also, in this code, and theSizes variable is green, but theWho variable is not. Plus, the error is happening in multiple places for NSUIntegers, NSMutableArray etc.
Where am I going wrong? Some of the header file code is below.
// TheSize.h
#class TheWho;
#interface TheSize : NSManagedObject
{
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *amount;
#property (nonatomic, retain) TheWho *theWho;
#property (nonatomic, retain) NSNumber *displayOrder;
#end
and..
//
// TheWho.h
//
#interface ImageToDataTransformer : NSValueTransformer {
}
#end
#interface TheWho : NSManagedObject {
}
#property (nonatomic, retain) NSString *instructions;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSSet *theSize;
#property (nonatomic, retain) UIImage *thumbnailImage;
#property (nonatomic, retain) NSManagedObject *image;
#property (nonatomic, retain) NSManagedObject *type;
#end
#interface TheWho (CoreDataGeneratedAccessors)
- (void)addTheSizesObject:(NSManagedObject *)value;
- (void)removeTheSizesObject:(NSManagedObject *)value;
- (void)addTheSizes:(NSSet *)value;
- (void)removeTheSizes:(NSSet *)value;
#end
I checked my declarations again, and I had to add an "s" to the NSSet *theSize entry. Those errors are gone. Thanks all for the help.
You still need to define the member variables. The #property directive declares the accessor methods, not the underlying members.
#interface TheWho : NSManagedObject {
NSString *instructions;
NSString *name;
NSSet *theSize;
UIImage *thumbnailImage;
NSManagedObject *image;
NSManagedObject *type;
}
You're trying to access theWho.theSizes but according to your header file, you should be trying theWho.theSize (without the trailing s).
Unless, there's a typo in how you've typed it here, and it wasn't copied directly.