This is my first objective code. I tried and searched to my best abilities but I did not get a solution to my problem. This is my code;
#import <Foundation/Foundation.h>
#interface XYZPerson:NSObject
#property NSString *firstname;
#property NSString *lastname;
#property NSString *middlename;
-(void)saysomething;
-(instancetype) initWithfirst:(NSString *) firstname
middlename:(NSString *) middlename
lastname:(NSString *) lastname;
+(void) greeting;
#end
#import "Myproj.h"
#implementation XYZPerson
-(instancetype) initWithfirst:(NSString *) firstname
middlename:(NSString *) middlename
lastname:(NSString *) lastname
{
self=[super init];
if(self)
{
_firstname=firstname;
_middlename=middlename;
_lastname=lastname;
}
return self;
}
-(void) saysomething
{
NSLog(#"Hi there %# %# %#",_firstname,_middlename,self.lastname);
[XYZPerson greeting];
}
+(void) greeting
{
NSLog(#"Have a great evening \n");
}
#end
When I try to create an object and initialize the properties its giving me an error
No visible #interface for 'XYZPerson' declares selector 'initWithfirst:middlename:lastname'
What I don't understand is it keeps prompting me another init method that I used previously (as seen in the screen shot)
I am unable to figure out the mistake, can somebody please help.
Thanks in advance
Related
In my AppDelegate.m, I am doing something like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#try {
// initalizing Meeting config
MeetingConfig *config = [[MeetingConfig alloc] init];
NSLog(#"Initalized Meeting Config: %#", config);
[config setRoomName:#"test123"];
NSLog(#"SetRoom name for Meeting config: %#", config.roomName);
NSString *clientId = #"";
NSLog(#"Unused Client id is: %#", clientId);
//Call UIView from here
}#catch (NSException *exception) {
NSLog(#"exception: %#", exception);
}
return YES;
}
Where my MeetingConfig.m file looks like this
#implementation MeetingConfig
- (id) init
{
if (self = [super init]) {
self.apiBase = #"https://api.in";
self.showSetupScreen = false;
self.autoTune = true;
}
return self;
}
- (void) setAuthToken:(NSString *)authToken
{
self.authToken = authToken;
}
- (void) setApiBase:(NSString *)apiBase
{
self.apiBase = apiBase;
}
// more code
and MeetingConfig looks like this
#import <Foundation/Foundation.h>
#interface MeetingConfig : NSObject
#property (nonatomic, assign) NSString* roomName;
#property (nonatomic, assign) NSString* authToken;
#property (nonatomic, assign)Boolean autoTune;
#property (nonatomic, assign)NSString* apiBase;
#property (nonatomic, assign)Boolean showSetupScreen;
- (void) setRoomName:(NSString *)roomName;
- (void) setAuthToken:(NSString *)authToken;
- (void) setShowSetupScreen:(Boolean)showSetupScreen;
- (void) setAutoTuneEnabled:(Boolean)autoTune;
- (id) init;
#end
Can someone help me in determining what I could be doing wrong here? and why doesn't it log exception in NSLog? Also, I am super new to objective C (i have been asked to stick with Objective c) and if anyone have any suggestion in regards to the code then please let me know.
Error
You're using assign for reference/pointer types: #property retain, assign, copy, nonatomic in Objective-C
They should probably be declared copy, because this is a kind of value object, I think.
No exceptions were caught because no exceptions were thrown. Throwing/catching exceptions for control flow is not common in Objective-C
You don't need to write explicit setter functions for #properties
You should prefer to use BOOL type instead of Boolean, with values of YES/NO instead of true/false.
You should return instancetype not id from init, at least in reasonably modern Objective C
Consider making an initialiser that takes all the properties (initWithRoomName:clientID:) and make them read only once set
You don't need to declare -(id) init in your header since it gets that from NSObject
I would like to set an text in the text field in another class and get it from another class. This is something what I want, but it does not work. Can you please help me. Thank you!
aaa.h
#import <Cocoa/Cocoa.h>
#interface aaa : NSImageView {
IBOutlet NSTextField *message;
}
#property (nonatomic, retain) IBOutlet NSTextField *message;
#end
aaa.m
#import "aaa.h"
#import "bbb.h"
#implementation aaa
#synthesize message;
- (void)awakeFromNib {
// [message setStringValue:#"ok, this works!"]; //but i don't want it from here
[self hello];
}
#end
bbb.h
#import <Foundation/Foundation.h>
#interface NSObject (bbb)
- (void)hello;
#end
bbb.m
#import "bbb.h"
#import "aaa.h"
#implementation NSObject (bbb)
- (void)hello {
aaa *obj = [[[aaa alloc] init] autorelease];
[obj.message setStringValue:#"This doesn't work :("]; // set text here, dont work.
NSLog(#"TEST: %#", [obj.message stringValue]);
}
#end
You are using category, so first thing it is used for extending the functionality of existing class. So you cannot set textfield value inside category. But else you can add some functonality after extracting the value. So you have to pass the value inside the category first. Try like this below:
- (void)awakeFromNib {
NSString *resultString=[self hello:#"This doesn't work :("];
[message setStringValue:resultString];
}
#end
#interface NSObject (bbb)
- (NSString*)hello:(NSString*)yourString;
#end
#implementation NSObject (bbb)
- (NSString*)hello:(NSString*)yourString {
return yourString;
}
#end
Actually, the problem is in
#property (nonatomic, copy) NSMutableArray* arrayOfObjects;
should be:
#property (nonatomic, strong) NSMutableArray* arrayOfObjects;
yeah? Thank,s for that guys!
I do not exactly understand how this bug worked, but I will try to get to know :)
I have trying to solve one problem since over an hour... I am afraid it is something very simple, but something I do not understand yet. I am guessing that this is something with memory management stuff cause this is my definitely weak point yet.
Post a little bit similar to
'Unrecognized selector sent to instance'
and some others but they didnt solved my problem...
In a nutshell (pasting much cause don't know where the potential bug is):
#interface MyCustomObject : NSObject {
NSString* name;
int birthDate;
double heightInMeters;
}
#property(strong, nonatomic) NSString * name;
#property(nonatomic) int birthDay;
#property(nonatomic) double heightInMeters;
-(id)initWithDate:(int)birthDate
AndName:(NSString *)nameString
AndHeight:(double)h;
#end
//////////////////////////////////
#import "MyCustomObject.h"
#implementation MyCustomObject
#synthesize name;
#synthesize birthDay;
#synthesize heightInMeters;
-(id)initWithDate:(int)bd AndName:(NSString *)nameString AndHeight:(double)h{
if(self = [super init]){
self.birthDay = bd;
self.name = nameString;
self.heightInMeters = h;
return self;
}
return nil;
}
#end
And some kind of database for MyCustomObjects:
DataCollection.h:
#interface DataCollection : NSObject{
NSMutableArray* arrayOfObjects;
}
#property (nonatomic, copy) NSMutableArray* arrayOfObjects;
-(void)addElement:(id)el;
#end
And implementation:
#import "DataCollection.h"
#implementation DataCollection
#synthesize arrayOfObjects;
-(id)init{
if(self = [super init]){
self.arrayOfObjects = [[NSMutableArray array] init];
NSLog(#"Number of record before init: %d", [self.arrayOfObjects count]);
[self addElement:[[MyCustomObject alloc] initWithDate:1988 AndName:#"Georg" AndHeight:1.88]];
[self addElement:[[MyCustomObject alloc] initWithDate:1951 AndName:#"Sebastian" AndHeight:1.58]];
NSLog(#"Number of record before init: %d", [self.arrayOfObjects count]);
return self;
}
return nil;
}
-(void)addElement:(id)el{
// UNRECOGNIZED SELECTOR ????
[self.arrayOfObjects addObject:el];
}
#end
Result is:
2013-03-05 15:42:56.826 XMLTest[11787:207] Number of record before
init: 0 Current language: auto; currently objective-c 2013-03-05
15:43:51.446 XMLTest[11787:207] -[__NSArrayI addObject:]: unrecognized
selector sent to instance 0x6816110
Do you see what I am doing wrong? I am guessing that this is something with memory management stuff
If you change
#property (nonatomic, copy) NSMutableArray* arrayOfObjects;
to
#property (nonatomic, strong) NSMutableArray* arrayOfObjects;
That should fix your problem
I didn't actually read your question - it's too long, but I think I may know your problem. Looking only at the debugger output at the end of your post, it looks like you're trying to send addObject: to an NSArray object. If you want to add and remove objects to an array, you need to use NSMutableArray instead.
So I'm at the point in my app where I need to replace the image of a sprite and I think I know how to do it, but I'm having trouble implementing it. Basically, I'm making a domino game and I need to be able to flip the domino over so you can see the numbers.
Starting with my domino.h file....
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Domino : CCSprite {
int int_leading;
int int_trailing;
int int_suitrank;
int int_playerid;
NSString *str_tilename;
NSString *str_mirrortilename;
}
#property int int_leading,int_trailing, int_playerid,
#property(nonatomic, retain) NSString *str_tilename;
#property(nonatomic, retain) NSString *str_mirrortilename;
-(void) print;
-(void) setTileName: (NSString *) theTileName;
-(void) setMirrorName: (NSString *) theMirrorName;
-(NSString *) str_tilename;
-(NSString *) str_mirrortilename;
#end
and then my .m file...
#import "Domino.h"
#implementation Domino
#synthesize int_leading,int_trailing, str_tilename, str_mirrortilename, int_playerid;
-(void) print {
NSLog (#"%i/%i", int_leading, int_trailing);}
-(void) setTileName: (NSString *) theTileName;
{
str_tilename=[[NSString alloc] initWithString: theTileName];
}
-(void) setMirrorName: (NSString *) theMirrorName;
{
str_mirrortilename=[[NSString alloc] initWithString: theMirrorName];
}
-(NSString *) str_tilename
{
return str_tilename;
}
-(NSString *) str_mirrortilename
{
return str_mirrortilename;
}
#end
Finally, in my game layer...
Domino *d06 =[[Domino alloc] initWithSpriteFrameName:#"blank.png"];
TileName= #"0-6.png";
MirrorName= #"6-0.png";
[d06 setTileName: TileName];
[d06 setMirrorName: MirrorName];
d06.int_leading=0;
d06.int_trailing=6;
At this point, I add all the dominoes together into a large mutable array that tracks which ones are still available for players to pick. The problem I'm having is that I either haven't found how to pull out the "TileName" from the mutable array, or maybe I have found it, but not understood it.
If I'm in a For loop, I would think the code should be something like
NSString *temp1=[[[movableSprites objectAtIndex:i]valueForKey:#"str_tilename"]string];
But this just results in the program crashing. Can you point me in the right direction?
NSString *temp1=[(Domino *)[movableSprites objectAtIndex:i] str_tilename];
I think using this code will solve your stuff... Check for the syntax.. Hope this helps.. :)
I keep reading that dot syntax is possible but I keep getting errors that the struct does not contain members I am referencing. Perhaps its not the dot syntax so I have included details of what I am doing in hopes of a solution:
// MobRec.h - used as the objects in the MobInfo array
#import <Foundation/Foundation.h>
#interface MobRec : NSObject {
#public NSString *mName;
#public int mSpeed;
}
#property (nonatomic, retain) NSString *mName;
#property (nonatomic) int mSpeed;
// MobDefs.h - array of MobRecords
#interface Mobdefs : NSObject {
#public NSMutableArray *mobInfo;
}
#property(assign) NSMutableArray *mobInfo; // is this the right property?
-(void) initMobTable;
#end
// MobDefs.m
#import "Mobdefs.h"
#import "Mobrec.h"
#implementation Mobdefs
#synthesize mobInfo;
-(void) initMobTable
{
// if I use traditional method I get may not respond
[mobInfo objectAtIndex:0 setmName: #"doug"];
// if I use dot syntax I get struct has no member named mName
mobInfo[1].MName = #"eric";
}
// main.h
MobDefs *mobdef;
// main.m
mobdef = [[Mobdefs alloc] init];
[mobdef initMobTable];
although both methods should work I get erros on both. What am I doing wrong? My best thoughts have been that I am using the wrong #property but I think I have tried all. I am performing alloc in main. Ideally I would like to for this use dot syntax and cant see why its not allowing it.
A couple of things: (edit: original point #1 removed due to error)
Although the dot syntax is supported, the array index syntax for NSArray is not. Thus, your call to mobInfo[1] will not be the same as [mobInfo objectAtIndex:1]; Instead, mobInfo will be treated as a simple C-style array, and that call would be almost guaranteed to result in a crash.
You should not define variables in your header file as you do in main.h. The line MobDefs *mobdef; belongs somewhere in main.m.
edit: Here is how it should look:
MobRec.h
#interface MobRec : NSObject {
NSString *mName;
int mSpeed;
}
#property (nonatomic, retain) NSString *mName;
#property (nonatomic) int mSpeed;
MobRec.m
#implementation MobRec
#synthesize mName;
#synthesize mSpeed;
#end
MobDefs.h
#interface MobDefs : NSObject {
NSMutableArray *mobInfo;
}
#property(assign) NSMutableArray *mobInfo;
-(void) initMobTable;
#end
MobDefs.m
#import "MobDefs.h"
#import "MobRec.h"
#implementation MobDefs
#synthesize mobInfo;
-(void) initMobTable
{
// option 1:
[(MobRec*)[mobInfo objectAtIndex:0] setMName:#"doug"];
// option 2:
(MobRec*)[mobInfo objectAtIndex:0].mName = #"eric";
// option 3:
MobRec *mobRec = [mobInfo objectAtIndex:0];
mobRec.mName = #"eric";
}
main.m
MobDef *mobdef = [[MobDefs alloc] init];
[mobdef initMobTable];
...
[mobdef release]; // don't forget!
You need to either cast the object returned by -objectAtIndex:, or use a method call on it:
[[mobInfo objectAtIndex: 0] setMName: #"doug"];
or
((Mobrec *) [mobInfo objectAtIndex: 0]).MName = #"doug";
[mobInfo objectAtIndex:0 setmName: #"doug"];
There is no objectAtIndex:setmName method, so you're going to have to explain what you think this is even supposed to do.
mobInfo[1].MName = #"eric";
Use objectAtIndex to look something up in an NSArray object.