Terminating app due to uncaught exception 'NSUnknownKeyException' : iOS app crash [duplicate] - iphone

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 9 years ago.
I'm a beginner programmer working on iPhone apps. I was wondering if anybody could help me with a bug in my app. When programming on XCode I found that my app suddenly crashed on launch after adding a little more code. The output panel showed the following error:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key play.'
* First throw call stack:
(0x1c91012 0x10cee7e 0x1d19fb1 0xb7ae41 0xafc5f8 0xafc0e7 0xb26b58 0x230019 0x10e2663 0x1c8c45a 0x22eb1c 0xf37e7 0xf3dc8 0xf3ff8 0xf4232 0x433d5 0x4376f 0x43905 0x8eceab6 0x4c917 0x1096c 0x1194b 0x22cb5 0x23beb 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1117a 0x12ffc 0x222d 0x2155)
libc++abi.dylib: terminate called throwing an exception
(lldb)
The code that shows up when the app terminates is as follows
#import <UIKit/UIKit.h>
#import "CIAAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CIAAppDelegate class]));
}
}
My ViewController.h shows:
#import <UIKit/UIKit.h>
#interface CIAViewController : UIViewController {
IBOutlet UILabel *cruzia;
IBOutlet UILabel *textarea;
IBOutlet UIButton *playbtn;
IBOutlet UIButton *tutorialbtn;
IBOutlet UIButton *optionsbtn;
IBOutlet UIButton *trainingbtn;
IBOutlet UIButton *back;
}
-(IBAction)press;
-(IBAction)press2;
-(IBAction)press3;
-(IBAction)press4;
-(IBAction)press5;
Also my ViewController.m shows
#import "CIAViewController.h"
#interface CIAViewController ()
#end
#implementation CIAViewController
-(IBAction)press {
cruzia.hidden = 0;
textarea.hidden = 0;
playbtn.hidden = 1;
tutorialbtn.hidden = 1;
optionsbtn.hidden = 1;
trainingbtn.hidden = 1;
back.hidden = 0;
cruzia.text = #"Play";
textarea.text = #"Hello! You are playing the game of Cruzia!";
}
-(IBAction)press2 {
cruzia.hidden = 0;
textarea.hidden = 0;
playbtn.hidden = 1;
tutorialbtn.hidden = 1;
optionsbtn.hidden = 1;
trainingbtn.hidden = 1;
back.hidden = 0;
cruzia.text = #"Tutorial";
textarea.text = #"Welcome! You are watching the Cruzia tutorial!";
}
-(IBAction)press3 {
cruzia.hidden = 0;
textarea.hidden = 0;
playbtn.hidden = 1;
tutorialbtn.hidden = 1;
optionsbtn.hidden = 1;
trainingbtn.hidden = 1;
back.hidden = 0;
cruzia.text = #"Options";
textarea.text = #"You are now in the Options screen. You can edit the settings of the game!";
}
-(IBAction)press4 {
cruzia.hidden = 0;
textarea.hidden = 0;
playbtn.hidden = 1;
tutorialbtn.hidden = 1;
optionsbtn.hidden = 1;
trainingbtn.hidden = 1;
back.hidden = 0;
cruzia.text = #"Training";
textarea.text = #"This is the training area. You can improve your Cruzia skills here!";
}
-(IBAction)press5 {
cruzia.hidden = 0;
textarea.hidden = 0;
playbtn.hidden = 0;
tutorialbtn.hidden = 0;
optionsbtn.hidden = 0;
trainingbtn.hidden = 0;
back.hidden = 1;
cruzia.text = #"Cruzia";
}
- (void)viewDidLoad
{
textarea.hidden = 1;
playbtn.hidden = 0;
tutorialbtn.hidden = 0;
optionsbtn.hidden = 0;
trainingbtn.hidden = 0;
back.hidden = 1;
cruzia.text = #"Cruzia";
[super viewDidLoad];
}
#end
I am sorry to waste your time but I have had this error for a long time and would like it if you had a solution.

What The Problem Is:
At runtime your view controller is going to get deserialized from its XIB/Storyboard. During this process, it's going to read all the objects that are in your IB files and start instantiating them with an NSCoder. As it is instantiating those objects in your view, it's going to read the IBOutlet and IBAction connections in your storyboard and start assigning the hydrated objects to those properties that were connected at design time so that you have programmatic access to them at run time. But here's the kicker: If you have a connection that points to a property that doesn't exist, then this exception will be thrown because it was told a property was there that really isn't.
What's happening is that your Storyboard/XIB is expecting your class to have a property called play. As your interface shows, you do not have a property called that. I suspect though, that you used to, but renamed it playbtn.
This is what is meant by
this class is not key value coding-compliant for the key play
It means it tried to do something like this under the covers:
[self setValue:button forKey:#"play"]
For additional enlightenment about Key/Value Coding and serialization, grab a comfy chair and read Key Value Coding Programming Guide and Archives and Serializations Programming Guide
How To Fix It:
In Interface Builder, right click on your view controller and see the list of connections:
See how there is a connection for the property play and it has a little yellow triangle next to it? This is Interface Builder trying to warn you that you have a connection set there, but the header isn't showing that there is an IBOutlet of that name.
All you need to do is click that little x to break the connection to the missing property.
How To Prevent This In The Future
Anytime you change a property in your code, you'll need to make sure that you break any associated connection in Interface Builder.

Related

How to extend my method that checking if 2 cards are matched to 3 cards that are matched?

I'm following the Stanford course, and we had to build a method for the app that checks for 2 cards matching, this is how the model that have the logic looks like (the method to look there is flipCardAtIndex):
#import "CardMatchingGame.h"
#import "PlayingCardsDeck.h"
#interface CardMatchingGame()
#property (readwrite, nonatomic) int score;
#property (strong, nonatomic) NSMutableArray *cards;
#property (strong, nonatomic) NSString *notification;
#end
#implementation CardMatchingGame
-(NSMutableArray *) cards {
if (!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
-(id)initWithCardCount:(NSUInteger)count usingDeck:(Deck *)deck {
self = [super init];
if (self) {
for (int i = 0; i < count; i++) {
Card *card = [deck drawRandonCard];
if (!card) {
self = nil;
} else {
self.cards[i] = card;
}
}
}
return self;
}
-(Card *) cardAtIndex:(NSUInteger)index {
return (index < self.cards.count) ? self.cards[index] : nil;
}
#define FLIP_COST 1
#define MISMATCH_PENALTY 2
#define BONUS 4
-(void) flipCardAtIndex:(NSUInteger)index {
Card *card = [self cardAtIndex:index];
if (!card.isUnplayable) {
if (!card.isFaceUp) {
for (Card *otherCard in self.cards) {
if (otherCard.isFaceUp && !otherCard.isUnplayable) {
int matchScore = [card match:#[otherCard]];
if (matchScore) {
otherCard.unplayble = YES;
card.unplayble = YES;
self.notification = [NSString stringWithFormat:#"%# & %# match!", card.contents, otherCard.contents];
self.score += matchScore * BONUS;
} else {
otherCard.faceUp = NO;
self.score -= MISMATCH_PENALTY;
self.notification = [NSString stringWithFormat:#"%# did not matched to %#", card.contents, otherCard.contents];
}
break;
}
}
self.score -= FLIP_COST;
}
card.faceUp = !card.isFaceUp;
}
}
#end
And this is the class model of the whole game, that got the actual matching method:
#import "PlayingCards.h"
#implementation PlayingCards
#synthesize suit = _suit;
//overriding the :match method of cards to give different acore if its only a suit match or a number match
-(int)match:(NSArray *)cardToMatch {
int score = 0;
if (cardToMatch.count == 1) {
PlayingCards *aCard = [cardToMatch lastObject];
if ([aCard.suit isEqualToString: self.suit]) {
score = 1;
} else if (aCard.rank == self.rank) {
score = 4;
}
}
return score;
}
//more stuff...
W already created it with an array so we will be able to extend it for more objects, but now i'm trying to figure out how do I extend it :/
This is my github for the project https://github.com/NirOhayon/Matchismo
i'm new to objective C and would appreciate it munch if you could help me to figure it out.
Thanks a bunch
You can chain these with a loop to check them all. Very basic way of doing it. Just loop through each card and check it against the "self" card that you have and increment the score instead of setting it.
-(int)match:(NSArray *)cardToMatch {
int score = 0;
for(int i = 0; i < cardToMatch.count; i++) {
PlayingCards *aCard = cardToMatch[i];
if ([aCard.suit isEqualToString: self.suit]) {
score += 1;
} else if (aCard.rank == self.rank) {
score += 4;
}
}
return score;
}
For flipCardAtIndex: , I would change it to flipCardsAtIndexes:(NSArray*)indexes, where indexes is an NSArray of NSNumbers. Then I would run a for loop checking and removing any cards that are unplayable or faceup, and pass through the remaining cards at those indexes to check match, and retrieve the match score.
To tell your view controller to add another card depends on how you have your view controller set up. You could do a protocol in a method that your view controller becomes the delegate of, and through a protocol method, tell it to switch. It could also be simpler than that, depending on how it checks your model of cards to decide what to show, if it sees three cards available instead of two, it could switch.
As the point of this exercise is to learn iOS programming, I want to give you a good head start and you should tweak and figure some stuff out on your own. I have a feeling you're a novice at programming, and if you are, you'll be surprised how much programming at your stage is trial and error. Eventually it will become second nature.

How Do i create UIimageView Dynamically (XCODE)

Hi everybody I making an Xcode App but have now been stuck for 2 days
in my app I have to create some images (which balls) in the amount stated by the user .
e,g if the user says make 10 10 balls should appear on the same screen at random locations I know to make location random but can't figure how to do it ... how do i connect to the interface builder ???
tried this
1.h
UIImageView *c[100];
1.m
for (int i = 0; i < "users value" ; i++) {
float x = (float)(67 + ((95 + 22) * i));
UIImageView *c[i] = [[UIImageView alloc]
initWithFrame:CGRectMake(x, 20.0f, 95.0f, 149.0f)];
[ballsView[i]setImage:[UIImage imageNamed:#"1.png"]];
ballsView[i].opaque = YES;
ballsView[i].hidden = NO; /* change to YES for distribution */
}
use this. hope helps
for (int i = 0; i < "users value" ; i++)
{
float x = (float)(67 + ((95 + 22) * i));
UIImageView *c[i] = [[UIImageView alloc]
initWithFrame:CGRectMake(x, 20.0f, 95.0f, 149.0f)];
c[i].image=[UIImage imageNamed:#"1.png"]];
[ballsView[i] addSubView:c[i]];
ballsView[i].opaque = YES;
ballsView[i].hidden = NO; /* change to YES for distribution */
}

Cocos2D: Referencing a classes TMXLayer from another class

Using Cocos2D. I've currently subclassed much of the player sprite and behaviors away from a debug map, and now I am having trouble referencing a TMXLayer from the sprite class. I've tried allocating an instance of the DebugZoneLayer class inside this method from the sprite class and releasing it with strange results. This compiles without errors, but fails to test the if (blocksCollidableGID | blocksCollidable2GID){ conditional because debugZoneLayer.blocksCollidable doesn't mean really anything to this method right now.
Method inside sprite class:
-(BOOL) checkTileCollisionForStrafing:(NSString*)omit{
for(int j = 0; j < _collisPushPointsNums; j++){
NSValue *val = [_collisPushPoints objectAtIndex:j];
CGPoint p = [val CGPointValue];
CGPoint tileCoord;
if(omit==#"y"){
tileCoord = [debugZoneLayer tileCoordForPosition:ccp(_heroSprite.position.x+_vel.x+p.x, _heroSprite.position.y+(p.y+.001))];
}else{
tileCoord = [debugZoneLayer tileCoordForPosition:ccp(_heroSprite.position.x+p.x, _heroSprite.position.y+_vel.y+p.y)];
}
int blocksCollidableGID = [debugZoneLayer.blocksCollidable tileGIDAt:tileCoord];
int blocksCollidable2GID = [debugZoneLayer.blocksCollidable2 tileGIDAt:tileCoord];
if (blocksCollidableGID | blocksCollidable2GID){
NSLog(#"j = %i", j);
return YES;
}
}
return NO;
}

What should be passed into a :(NSString *)text

Feeling a little confused. I am trying to pass a NSString as an argument to this method
-(void) setRightLabelText:(NSString *)text
{
rightLabel.text = text;
}
The code i use to call the method
for(int index=0; index<5; index++)
{
NSNumber *num = [card.statsArray objectAtIndex:index];
StatView *statView = (StatView *)[self.frontView viewWithTag:10+index];
NSString *nameHolder = #"test";
[statView setRightLabelText:nameHolder];
}
The code I used to create the View :
for(int i=0; i<totalButtons; i++)
{
StatView *sv = [[StatView alloc] initWithYPos:ypos];
sv.tag = 100 + i;
[sv.overlayButton addTarget:self action:#selector(statTapped:)
forControlEvents:UIControlEventTouchUpInside];
sv.overlayButton.tag = 10 + i;
[self.frontView addSubview:sv];
ypos += 26;
}
This to me looks perfect, but i get a crash when I get to this method call in the app.
Error Msg :
-[UIButton setRightLabelText:]: unrecognized selector sent to instance 0x5d116e0
2010-09-13 11:39:44.761 LeinsterRugby[1387:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setRightLabelText:]: unrecognized selector sent to instance 0x5d116e0'
StatView *statView = (StatView *)[self.frontView viewWithTag:10+index]; returns an UIButton instead of a StatView because the tag matches the one you assign to the button with sv.overlayButton.tag = 10 + i;.
It looks as though you're sending the setRightLabelText message (selector) to an instance of UIButton rather than an object of the type that you're implementing.
Rather than:
[statView setRightLabelText:nameHolder];
Do you perhaps mean:
[self setRightLabelText:nameHolder];
Probable reason for the crash would be
StatView *statView = (StatView *)[self.frontView viewWithTag:10+index];
Your computation to get the StatView from (10+index) is wrong.
Calculate your index properly.

std::list problem for iPhone development

I found some problems for std::list when I'm doing some iPhone development under Xcode. Here is the code:
///////////// interface //////////////
class CObj
{
public:
int value;
};
typedef std::list<CObj*> ObjList;
#interface testAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ObjList m_objs;
}
//////////// implementation /////////
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
for (int i = 0; i< 10; i++ )
{
CObj* obj = new CObj;
obj->value = i;
m_objs.push_back(obj);
}
NSLog(#"%d objects",m_objs.size() );
ObjList::iterator it = m_objs.begin();
while (it != m_objs.end())
{
CObj* obj = *it;
if ( obj->value == 3 )
it = m_objs.erase(it);
else
it++;
}
NSLog(#"%d objects",m_objs.size() );
}
The application simply crashes at m_objs.push_back. But if I change the std::list to std::vector, everything's fine. Any idea? Thanks
By default Objective-C does not call the constructor for c++ types on initialization.
If you set this flag in the build settings "Call C++ Default Ctors/Dtors in Objective-C", it will change the default behavior. Be aware that the flag only shows up if you have the target set to Device.