NSCFNumber unrecognized selector - iphone

i want to send data between views, but i get an error: unrecognized selector....
and the in the debugger, the variable mystring is a NSCFNumber ("at this time") instead of NSString...
allergy_appAppDelegate.h
#import <UIKit/UIKit.h>
#interface allergy_appAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
NSMutableArray *result_array;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (copy , readwrite) NSMutableArray *result_array;
#end
viewcontroller.m
allergy_appAppDelegate *dataCenter = (allergy_appAppDelegate *)[[UIApplication sharedApplication]delegate];
dataCenter.result_array = [[NSMutableArray alloc] initWithArray:Parser_result];
result.m
allergy_appAppDelegate *dataCenter = (allergy_appAppDelegate*)[[UIApplication sharedApplication]delegate];
show_user_array = [[NSMutableArray alloc] initWithArray: dataCenter.result_array]
for (NSString *mystring in show_user_array) {
textView.text = [[textView text] stringByAppendingString:#"\n"];
textView.text = [[textView text] stringByAppendingString:mystring];
}

Instance variables should be camel-cased, not have _. I.e. result_array should be resultArray. Classes start with capital letters.
Are you sure your result array is full of instances of NSString or NSNumber (or whatever you need)?
Given that you are leaking the array here...
dataCenter.result_array = [[NSMutableArray alloc] initWithArray:Parser_result];
... it is unlikely that this is an over-release problem. Note also that copy with NSMutableArray won't do what you want (the compiler should flag it, but doesn't). -copy always returns an immutable copy of an instance of a class cluster.

Related

using an NSArray in a custom class isn't working

Starting over. I am fairly new to objective C. I have created the following class and I can't figure out how to initialize the array.
Can anyone provide any guidance on how to initialize the NSArray?
StatusPost.m
#import "StatusPost.h"
#implementation StatusPost
#synthesize messageId, fromName, friendId, message, choice2, choice3, choice4, picture, fbImage, commentCount, commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString *) rPicture
fbImage:(UIImage *)rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray
{
if (self = [super init]) {
commentArray = [NSArray new];
self.messageId = rMessageId;
self.fromName = rFromName;
self.friendId = rFriendId;
self.message = rMessage;
self.choice2 = rChoice2;
self.choice3 = rChoice3;
self.choice4 = rChoice4;
self.picture = rPicture;
self.fbImage = rfbImage;
self.commentCount = rCommentCount;
self.commentArray = rCommentArray;
}
return self;
}
- (void)dealloc {
[messageId release];
[fromName release];
[friendId release];
[message release];
[picture release];
[fbImage release];
[commentCount release];
[commentArray release];
[super dealloc];
}
#end
StatusPost.h:
#import
#interface StatusPost : NSObject {
NSString* messageId;
NSString* fromName;
NSString* friendId;
NSString* message;
NSString* choice2;
NSString* choice3;
NSString* choice4;
NSString* picture;
UIImage* fbImage;
NSString* commentCount;
NSArray* commentArray;
}
#property (nonatomic, retain) NSString* messageId;
#property (nonatomic, retain) NSString* fromName;
#property (nonatomic, retain) NSString* friendId;
#property (nonatomic, retain) NSString* message;
#property (nonatomic, retain) NSString* choice2;
#property (nonatomic, retain) NSString* choice3;
#property (nonatomic, retain) NSString* choice4;
#property (nonatomic, retain) NSString* picture;
#property (nonatomic, retain) UIImage* fbImage;
#property (nonatomic, retain) NSString* commentCount;
#property (nonatomic, retain) NSArray* commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString*) rPicture
fbImage:(UIImage*) rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray;
#end
It is likely that you aren't ever initializing the array, so when you try to add an object, you are just sending a message to nil. In the custom class's init method, add the line:
commentArray = [NSMutableArray new];
[NSArray new] is shorthand for [[NSArray alloc] init], so, technically speaking, that statement "inits" the NSArray.
However, your code looks a bit peculiar. You have the following statements in your init:
commentArray = [NSArray new];
self.commentArray = rCommentArray;
The first statement is setting the instance variable commentArray to the address of the newly alloced/inited NSArray while the second is setting the property commentArray to a parameter value. However, you have (through the #synthesize) made the instance variable commentArray the "backing store" for the property commentArray, so when you execute the second line the effect of the first line is overwritten (and the NSArray you created is "leaked").
(But if your real question is how to "load" an NSArray with values, you should ask that question -- and you'll get different answers.)

instanced variable accessed in class method, Objective C

This is the original codes when I am getting this error
ClassA.h
+(ClassA*)methodA:(NSData*)data;
#property (nonatomic, strong) NSMutableArray *arr;
#property (nonatomic, strong) RXMLElement *rxmlRoot;
#end
ClassA.m
+(ClassA*)methodA:(NSData*)data {
//NSLog(#"class is %#",[name class]);
ClassA *ClassA = [[Stores alloc] init];
arr = [NSMutableArray array];
rxmlRoot = [RXMLElement elementFromXMLData:data];
}
I am reviewing my codes and now I have tow options to fix the code
OptionA
ClassA.h
+(ClassA*)methodA:(NSData*)data;
#property (nonatomic, strong) NSMutableArray *arr;
#property (nonatomic, strong) RXMLElement *rxmlRoot;
#end
ClassA.m
+(ClassA*)methodA:(NSData*)data {
//NSLog(#"class is %#",[name class]);
ClassA *ClassA = [[Stores alloc] init];
ClassA.arr = [NSMutableArray array]; <----- this has been modified
ClassA.rxmlRoot = [RXMLElement elementFromXMLData:data];<----- this has been modified
}
OptionB
ClassA.h
+(ClassA*)methodA:(NSData*)data;
#end
ClassA.m
static RXMLElement *rxlRoot; <----- this has been added as class variable
static NSMutableArray *arr; <----- this has been added as class variable
+(ClassA*)methodA:(NSData*)data {
//NSLog(#"class is %#",[name class]);
ClassA *ClassA = [[Stores alloc] init];
arr = [NSMutableArray array];
rxmlRoot = [RXMLElement elementFromXMLData:data];
}
I try either optionA or optionB and the compiler does not complain anything and the codes work properly.
I see many posts following the optionB which create class variables.
Question: is optionA also a good way to follow when we are dealing with class method and class variable as well.
Please advice me on this issue and by the way if I have made mistakes somewhere, please correct me. Thanks
There is no such thing as a "class variable" in Objective-C. When you declare a variable as static it is, effectively, a global variable that is limited to either the scope of the file it is compiled within or the scope of a method/function (or sub-expression).
Get rid of the Class. part of your calls to set the variables; that'll work in the .m file for the Class only; which is generally the pattern you would want (truly global variables are generally discouraged -- sometimes useful, though).
Create a designated initializer with a data parameter. Every class should have a designated initializer.
In your class method call the designated initializer passing in data.
Here is example code:
//ClassA.h
#class RXMLElement;
#interface ClassA : NSObject
+(ClassA*)methodA:(NSData*)data;
-(id)initWithData:(NSData*)data;
#property (nonatomic, strong) NSMutableArray *arr;
#property (nonatomic, strong) RXMLElement *rxmlRoot;
#end
//ClassA.m
#implementation ClassA
#synthesize arr;
#synthesize rxmlRoot;
+(ClassA*)methodA:(NSData*)data {
return [[ClassA alloc] initWithData:data];
}
-(id)initWithData:(NSData*)data {
self = [super init];
if (self) {
arr = [NSMutableArray array];
rxmlRoot = [RXMLElement elementFromXMLData:data];
}
return self;
}
#end
ARC is assumed in the example.

Unknown Memory Leak in iPhone

I am currently building an app for the iPhone and cannot figure out why I keep getting a memory leak to appear in the Leaks Instrument tool.
Here is the code and I have added comments to two places of where it is happening.
NSString *pathname = [[NSBundle mainBundle] pathForResource:self.toUseFile ofType:#"txt" inDirectory:#"/"];
//Line below causes a leak
self.rawCrayons = [[NSString stringWithContentsOfFile:pathname encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:#"\n"];
self.sectionArray = [NSMutableArray array];
for (int i = 0; i < 26; i++) [self.sectionArray addObject:[NSMutableArray array]];
for(int i=0; i<self.rawCrayons.count; i++)
{
self.string = [self.rawCrayons objectAtIndex:i];
NSUInteger firstLetter = [ALPHA rangeOfString:[string substringToIndex:1]].location;
if (firstLetter != NSNotFound)
{
NSInteger audio = AUDIONUM(self.string);
NSInteger pictures = PICTURESNUM(self.string);
NSInteger videos = VIDEOSNUM(self.string);
//Line below causes a leak
[[self.sectionArray objectAtIndex:firstLetter] addObject:[[Term alloc] initToCall:NAME(self.string):audio:pictures:videos]];
}
[self.string release];
}
Thanks in advance!
Edit
Here are my property declarations.
#property (nonatomic, retain) NSArray *filteredArray;
#property (nonatomic, retain) NSMutableArray *sectionArray;
#property (nonatomic, retain) UISearchBar *searchBar;
#property (nonatomic, retain) UISearchDisplayController *searchDC;
#property (nonatomic, retain) NSString *toUseFile;
#property (nonatomic, retain) NSArray *rawCrayons;
#property (nonatomic, retain) NSString *string;
#property (nonatomic, retain) TermViewController *childController;
Here are the leaks that are occurring after follow Nick Weaver's fixes.
Here is an expanded version of one of the NSCFString.
And another image.
Image with the Responsible Caller:
Also, because this may be useful, here are the properties for Term:
#property (nonatomic, retain) NSString *name;
#property (nonatomic) NSInteger numberAudio;
#property (nonatomic) NSInteger numberPictures;
#property (nonatomic) NSInteger numberVideos;
And the implementation:
#implementation Term
#synthesize name, numberAudio, numberPictures, numberVideos;
- (Term*)initToCall:(NSString*) toSetName:(NSInteger) audio:(NSInteger) pictures:(NSInteger) videos
{
self.name = [toSetName retain];
self.numberAudio = audio;
self.numberPictures = pictures;
self.numberVideos = videos;
return self;
}
- (NSString*)getName
{
return [[name retain] autorelease];
}
-(void)dealloc
{
[name release];
[super dealloc];
}
#end
Ok, try this changed Version of Temp. I've deleted the getter because you have already one by synthesizing. You cann use the getter like this for name:
term.name
The problem was how you set the name: you want a copy of the name and setting it with the synthesized setter without calling a retain should do the trick. You could, of course, have set it with the retained property of name but you should have left out retain, like this self.name = toSetName;. The setter will retain it for you.
#property (nonatomic, copy) NSString *name;
#property (nonatomic) NSInteger numberAudio;
#property (nonatomic) NSInteger numberPictures;
#property (nonatomic) NSInteger numberVideos;
#implementation Term
#synthesize name, numberAudio, numberPictures, numberVideos;
- (Term*)initToCall:(NSString*) toSetName:(NSInteger) audio:(NSInteger) pictures:(NSInteger) videos
{
self.name = toSetName;
self.numberAudio = audio;
self.numberPictures = pictures;
self.numberVideos = videos;
return self;
}
-(void)dealloc
{
[name release];
[super dealloc];
}
Adding an object to an array will retain the instance, so the retain is 2 because you call
[[Term alloc] initToCall..
Do something like
Term *term = [[Term alloc] initToCall..];
[theArray addObject:term];
[term release];
1. See the arrow in the first line in the address column? Click it!
2. After clicking :)
Hard to tell you why the first one is leaking, because we don't know what the property is declared as. Is it retain? copy? assign? what?
The last one is fairly self explanatory though, you're taking ownership of a Term object, and not releasing it when it's added. addObject: retains its argument, meaning if you don't need that Term anymore, you need to give up ownership. I.e., pass -autorelease to the result of your initToCall:::: (which btw is a very bad name for a method)
Change:
[[self.sectionArray objectAtIndex:firstLetter] addObject:[[Term alloc] initToCall:NAME(self.string):audio:pictures:videos]];
to:
Term *tempTerm = [[Term alloc] initToCall:NAME(self.string):audio:pictures:videos];
[[self.sectionArray objectAtIndex:firstLetter] addObject:tempTerm];
[tempTerm release];
By alloc'ing an object you are responsible for it's release.

EXC_BAD_ACCESS when calling class init in Objective C

I've been trying to figure this out but I can't figure out what I'm doing wrong.
I wrote a class and whenever I try to initialize it, I get a EXC_BAD_ACCESS error. I can't even step into the initialization.
Anyone have any idea what I'm doing wrong?
User *myUser = [myUser init];
.h file:
#import <Foundation/Foundation.h>
#interface User : NSObject {
long rowId;
NSString *email;
NSString *password;
NSString *fileVersion;
}
#property long rowId;
#property (assign) NSString *email;
#property (assign) NSString *password;
#property (assign) NSString *fileVersion;
#end
.m file
#import "User.h"
#implementation User
#synthesize rowId, email, password, fileVersion;
-(id)init {
self = [super init];
return self;
}
#end
You have to actually allocate the object:
User *myUser = [[User alloc] init];
Don't forget to release it when you're done using it.

Cannot figure out how to get rid of memory leak

I'm trying to test for memory leaks in my iphone and I'm not having much luck getting rid of this one. Here is the code that is leaking.
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)aMoc delegate:(id)aDelegate runSync:(BOOL)aRunSync {
if (self = [super init]) {
self.moc = aMoc;
self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc];
self.delegate = aDelegate;
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.runSync = aRunSync;
}
return self;
}
It is leaking on the self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc]; line.
The self.settingManager instance variable is released in the dealloc method of the class.
I'm not sure what other information would be pertinent. Please let me know and I can provide it.
Thanks for any assistance.
-Mark
Here is the header file.
#interface VacaCalcCalculation : NSObject {
NSManagedObjectContext *moc;
VacaCalcSettingsManager *settingsManager;
id delegate;
NSCalendar *calendar;
NSDate *nextBankLimitDate;
BOOL runSync;
}
#property (nonatomic, retain) NSManagedObjectContext *moc;
#property (nonatomic, retain) VacaCalcSettingsManager *settingsManager;
#property (nonatomic, retain) id delegate;
#property (nonatomic, retain) NSCalendar *calendar;
#property (nonatomic, retain) NSDate *nextBankLimitDate;
#property (nonatomic) BOOL runSync;
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)aMoc delegate:(id)aDelegate;
If your settingsManager property is set to retain then you are retaining an object twice with the line: self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc];
Try adding autorelease to the alloc]init or creating a class method on VacaCalcSettingsManager that returns an autoreleased object. Otherwise you could redeclare your property with assign so that it does not retain the object a second time.