I used the NSPredictate Class, but the following error has occurred.
I Can't know the reason.
Why does occur following error?
Follow is source code.
#import "Predictate.h"
#implementation Predictate
#synthesize dictate;
-(id)init{
if ((self = [super init])) {
}
return self;
}
- (void)Predictate{
dictate = [[NSMutableArray alloc]initWithObjects:#"AAA",#"BBB",#"CCC", nil];
NSPredicate *test = [NSPredicate predicateWithFormat:#"dictate like 'AAA'"];
NSMutableArray *result = [dictate filteredArrayUsingPredicate:test];
NSLog(#"%#",result);
}
-(void)dealloc{
[dictate release];
[super dealloc];
}
#end
Error message is below.
2012-01-02 00:57:39.972 filter[1750:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x100002290> valueForUndefinedKey:]: this class is not key value coding-compliant for the key dictate.'
You're using NSPredicate to filter an array of string objects, but are using dictate like 'AAA'. The predicate will have absolutely no idea what this dictate means.
You will want to replace dictate with SELF, so that it becomes "SELF like 'AAA'"
Related
I'm trying to make a simple subclass of CCNode, but I can't create the object.
It gives me the error "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[ContentPane<0x206898> init]: cannot init a class object.'"
Here is my subclass of CCNode:
#import "ContentPane.h"
#implementation ContentPane{
int content[8][4];
CCSprite *_rockPath1;
CCSprite *_rockPath2;
}
- (id)init {
self = [super init];
if (self) {
CCLOG(#"ContentPane created");
}
return self;
}
#end
Here is where I try to initiate it:
- (void)didLoadFromCCB {
// tell this scene to accept touches
self.userInteractionEnabled = TRUE;
_counter = 0;
ContentPane *pane = [ContentPane init];
}
Couple things,
In Obj-c when you want to initialize an Object, you need to allocate space for it.
That is done using the alloc keyword.
so your ContentPane *pane = [ContentPane init];
turns into ContentPane *pane = [[ContentPane alloc] init];
Also, whatever tutorial you are following, Stop... the way you have declared your variables we call them (iVars) is a very old fashioned way of doing things, they should really be properties. and Boolean values are represented by YES and NO not TRUE and FALSE
If you are here just like me wondering why you code is crashing.
[NSArray init];
should be :
[[NSArray alloc] init];
or
[NSArray array];
You crash could caused by any other class here NSArray here is for reference only.
I have this implementation file with aNSArray object userIDs
NSArray *userIDs;
NSInteger friendID;
#implementation TableViewController
-(void)reciveFriendsIDs:(NSArray *)array
{
userIDs = [NSArray arrayWithArray:array];
}
-(NSString *)getFriendId
{
return [userIDs objectAtIndex:friendID];
}
.
.
.
#end
and the method -(NSString *)getFriendId call it from another class like this :
TableViewController *tableController = [[TableViewController alloc]init];
NSString *fid = [tableController getFriendId];
But I am having an error said "-[__NSArrayI respondsToSelector:]: message sent to deallocated instance 0x20320200" and the compiler indicate the error in this line:
return [userIDs objectAtIndex:friendID];
You are allocating the NSArray with arrayWithArray static method.
In this way it's getting added in the auto release pool and the retain count will be 0.
Either retain it or manually alloc it with [[NSArray alloc] init]
I was getting the same exception on line
if(self.arrTypes != nil)
cause of the following line being used at a different place in code
[self.arrTypes release];
and replacing this code with
self.arrTypes = nil;
resolved the issue.
I do not understand what I am doing wrong. I have a dictionary as a property of a singleton class:
#interface CABResourceManager : NSObject
{
....
NSMutableDictionary* soundMap;
}
#property (retain) NSMutableDictionary *soundMap;
Then I add an object to this dictionary in one class method:
+ (void)loadSoundFromInfo:(ABSoundInfo)sound
{
static unsigned int currentSoundID = 0;
CABSound* newSound = [[CABSound alloc] initWithInfo:(ABSoundInfo)sound soundID:++currentSoundID];
[[CABResourceManager sharedResMgr].soundMap setObject:newSound forKey:sound.name];
}
And try to get it in another method:
+ (ALuint)playSoundByName:(NSString*)name
{
NSMutableDictionary* map = [CABResourceManager sharedResMgr].soundMap;
CABSound *sound = [map objectForKey:name]; // here comes the exception
and the app exits on exception by that.
2011-03-27 20:46:53.943 Book3HD-EN[5485:207] *** -[NSCFSet objectForKey:]: unrecognized selector sent to instance 0x226950
2011-03-27 20:46:53.945 Book3HD-EN[5485:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException'
I guess it might have something with memory management, but hier it looks clear for me: CABSound object is retained in dictionary by doing setObject(), it should not be released at this time.
I'd check that soundMap is properly initialized. It looks like soundMap is a bad pointer at the time you get the error. It might happen to be nil in +loadSoundFromInfo, which wouldn't produce an error right away.
Make sure that you've initialized your soundMap in designated initializer:
// - (id) init... or something else
soundMap = [[NSMutableDictionary alloc] init];
Dont forget to override default dealloc implementation:
// class implementation file
- (void)dealloc {
[soundMap release];
//...release other objects you own...
[super dealloc];
}
is almost a week that i can't fix a brain painful problem:
I have a UIViewController subclass named StreamingViewControllerCommon that implement this property:
#property (nonatomic, assign, readonly) BOOL isMusicStopped;
and of course i #synthesize it in the .m file
then i have 2 subclasses of this class: Listen_UIViewController and LastNews_UIViewController that modify (not calling self.isMusicStopped but accessing directly to it) the var isMusicStopped.
In another Class i have a NSMutableDictionary that contains 2 instance (1 for each class) of these two classes but when i try to do this:
if (streamingViews){
for(StreamingViewControllerCommon* aView in streamingViews){
BOOL stopped = aView.isMusicStopped;
NSLog(#"%#",stopped);
if(stopped){
[aView closeStream];
[streamingViews removeObjectForKey:[aView class]];
[aView release];
aView = nil;
}
}
}
i obtain this error:
2011-02-08 15:55:09.760 ProjectName[6182:307] +[LastNews_UIViewController isMusicStopped]: unrecognized selector sent to class 0x2143c
2011-02-08 15:55:09.768 ProjectName[6182:307] CoreAnimation: ignoring exception: +[LastNews_UIViewController isMusicStopped]: unrecognized selector sent to class 0x2143c
But the weird thing is that in the StreamingViewControllerCommon implement also these methods:
-(void) destroyStreamer{
}
-(void) closeStream{
[self destroyStreamer];
}
and when i do:
NSMutableArray* keys = [[NSMutableArray alloc] initWithArray:[streamingViews allKeys]];
[keys removeObject:[thisView class]];
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[streamingViews objectsForKeys:keys notFoundMarker:#"404"]];
if (tmp){
for (StreamingViewControllerCommon* vc in tmp)
[vc closeStream];
[tmp release];
}
[streamingViews removeObjectsForKeys:keys];
i am not getting any error and the subclasse's overridden closeStream methods are right called.
What am i doing wrong?
Best Regards, Antonio
EDIT: As I wrote in the comment: Changing this:
if (streamingViews){
for(StreamingViewControllerCommon* aView in streamingViews){
BOOL stopped = aView.isMusicStopped;
NSLog(#"%#",stopped);
if(stopped){
[aView closeStream];
[streamingViews removeObjectForKey:[aView class]];
[aView release];
aView = nil;
}
}
}
in this:
if (streamingViews){
for (id aViewClass in streamingViews){
StreamingViewControllerCommon* aView = [[streamingViews objectForKey:aViewClass] retain];
//NSLog(#"%#",aView.isMusicStopped);
if(aView.isMusicStopped){
[aView closeStream];
[streamingViews removeObjectForKey:aViewClass];
[aView release];
aView = nil;
}
}
}
Did the trick :P The for each cycle of a NSDictionary returns its keys not the objects and, since i was using the Class of objects as key i was obtaining that weird exception
For whatever reason, it looks like streamingViews contains not an instance of LastNews_UIViewController, but the class itself. That's what the plus sign in +[LastNews_UIViewController isMusicStopped] signifies.
so I have a class that I declare as a singleton and in that class I have a NSMutableArray that contains some NSDictionaries with some key/value pairs in them. The trouble is it doesn't work and I don't know why... I mean it crashes with EXC_BAD_ACCESS but i don't know where. I followed the code and it did create a new array on first add, made it to the end of the function ..and crashed ...
#interface dataBase : NSObject {
NSMutableArray *inregistrari;
}
#property (nonatomic,retain) NSMutableArray *inregistrari;
-(void)adaugaInregistrareCuData:(NSDate *)data siValoare:(NSNumber *)suma caVenit:(BOOL)venit cuDetaliu:(NSString *)detaliu;
-(NSDictionary *)raportIntreData:(NSDate *)dataInitiala siData:(NSDate *)dataFinala;
-(NSArray *)luniDisponibileIntreData:(NSDate *)dataInitiala siData:(NSDate *)dataFinala;
-(NSArray *)aniDisponibiliIntreData:(NSDate *)dataInitiala siData:(NSDate *)dataFinala;
-(NSArray *)vectorDateIntreData:(NSDate *)dataI siData:(NSDate *)dataF;
-(void)salveazaInFisier;
-(void)incarcaDinFisier;
+ (dataBase *)shareddataBase;
#end
And here is the .m file
#import "dataBase.h"
#import "SynthesizeSingleton.h"
#implementation dataBase
#synthesize inregistrari;
SYNTHESIZE_SINGLETON_FOR_CLASS(dataBase);
-(void)adaugaInregistrareCuData:(NSDate *)data siValoare:(NSNumber *)suma caVenit:(BOOL)venit cuDetaliu:(NSString *)detaliu{
NSNumber *v=[NSNumber numberWithBool:venit];
NSArray *input=[NSArray arrayWithObjects:data,suma,v,detaliu,nil];
NSArray *keys=[NSArray arrayWithObjects:#"data",#"suma",#"venit",#"detaliu",nil];
NSDictionary *inreg=[NSDictionary dictionaryWithObjects:input forKeys:keys];
if(inregistrari == nil) {
inregistrari=[[NSMutableArray alloc ] initWithObjects:inreg,nil];
}else {
[inregistrari addObject:inreg];
}
[inreg release];
[input release];
[keys release];
}
It made it to the end of that adaugaInregistrareCuData ... ok . said the array had one object ... and then crashed
Try adding "NSZombieEnabled" with value "YES" to your arguments on your executeable:
Right click your executeable, select get info and add that entry to the variables in the bottom list.
This will tell you what datatype has crashed.
Using build & analyze it tells me that you are releasing inreg, input and keys twice.
All three variables will be autoreleased, your manual release will cause the later autorelease to fail and give you your BAD_ACCESS.
Don't manually release them, remove these three lines from your code:
[inreg release];
[input release];
[keys release];