well, I have 2 classes and want to get NSString from class2 to class1.
where do I wrong?
class1.h
#import <Cocoa/Cocoa.h>
#import "class2.h"
#class class2;
#interface class1 : NSObject
-(IBAction)getstringfromclass2(id)sender;
#end
class1.m
#import "class1.h"
#implementation class1
-(IBAction)getstringfromclass2(id)sender {
class2 *controller = [[class2 alloc] init];
NSLog(#"%#", [controller getstring]);
}
#end
class2.h
#import <Cocoa/Cocoa.h>
#interface class2 : NSObject {
NSString *astring;
}
-(NSString)getstring;
#property (readwrite,retain) NSString *astring;
#end
class2.m
#import "class2.h"
#synthesize astring;
#implementation class2
-(NSString)getstring {
return [self astring];
}
#end
check your some expression. NSString -> NSString *
#import <Cocoa/Cocoa.h>
#interface class2 : NSObject {
NSString *astring;
}
-(NSString *)getstring;
#property (readwrite,retain) NSString *astring;
#end
class2.m
#import "class2.h"
#synthesize astring;
#implementation class2
-(NSString *)getstring {
return [self astring];
}
#end
You need to return a pointer to NSString:
-(NSString*)getstring {
return [self astring];
}
And:
-(NSString*)getstring;
Related
I have Two classes in have declared string in one variable and trying access from another class. have initiated getter setter in between them still getting null value when I try to print from subclass.
CustomerCarSelection.h
#import <UIKit/UIKit.h>
#interface CustomerCarSelection : UITableViewController <UITableViewDataSource, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *carListTableView;
- (IBAction)next:(id)sender;
#property NSString *carSelectedForService;
#end
customerCarSelection.m
#import "CustomerCarSelection.h"
#import <Parse/Parse.h>
#import "carSelectCellTableViewCell.h"
#import "ServiceResultView.h"
#interface CustomerCarSelection ()
#end
#implementation CustomerCarSelection
#synthesize carListTableView;
#synthesize carSelectedForService;
- (void)viewDidLoad {
[super viewDidLoad];
ServiceResultView *service = [[ServiceResultView alloc]init];
service.pointer = self;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell tabbed");
PFObject *temp = [cellDetails objectAtIndex:indexPath.row];
carSelectedForService = temp.objectId;
NSLog(#"%#", carSelectedForService);
}
serviceResultView.h
#import <UIKit/UIKit.h>
#import "CustomerCarSelection.h"
#interface ServiceResultView : UIViewController
{
CustomerCarSelection *pointer;
}
#property (nonatomic, retain) CustomerCarSelection *pointer;
- (IBAction)confirm:(id)sender;
#end
serviceResultView.m
#import "ServiceResultView.h"
#import "CustomerCarSelection.h"
#interface ServiceResultView ()
#end
#implementation ServiceResultView
#synthesize pointer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *carResult = pointer.carSelectedForService;
NSLog(#"%#", carResult);
}
Where am I making mistake. When the cell tabbed it will assign object id to the String i trying access that object from another class
CustomerCarSelection.h
#import <UIKit/UIKit.h>
#interface CustomerCarSelection : UITableViewController <UITableViewDataSource, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *carListTableView;
- (IBAction)next:(id)sender;
#end
customerCarSelection.m
#import "CustomerCarSelection.h"
#import <Parse/Parse.h>
#import "carSelectCellTableViewCell.h"
#import "ServiceResultView.h"
#interface CustomerCarSelection ()
#end
#implementation CustomerCarSelection
#synthesize carListTableView;
#synthesize carSelectedForService;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell tabbed");
PFObject *temp = [cellDetails objectAtIndex:indexPath.row];
serviceResultView *obj = [serviceResultView alloc] init];
obj.carSelectedForService = temp.objectId;
NSLog(#"%#", obj.carSelectedForService);
}
serviceResultView.h
#import <UIKit/UIKit.h>
#import "CustomerCarSelection.h"
#interface ServiceResultView : UIViewController
#property NSString *carSelectedForService;
- (IBAction)confirm:(id)sender;
#end
serviceResultView.m
#import "ServiceResultView.h"
#import "CustomerCarSelection.h"
#interface ServiceResultView ()
#end
#implementation ServiceResultView
#synthesize pointer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"%#", self.carSelectedForService);
}
I have a Player model:
Player.h:
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSMutableArray *answers;
#end
Player.m:
#import <Foundation/Foundation.h>
#import "Player.h"
#interface Player ()
#end
#implementation Player
- (id)init {
if (self = [super init]) {
self.answers = [[NSMutableArray alloc] init];
self.image = [UIImage imageNamed:#"defaultProfileImage"]; // <-------------- error
}
return self;
}
#end
However this displays the error: Use of undeclared identifier 'UIImage'
How would I use UIImage in my model?
You will need to import
#import <UIKit/UIKit.h>
So that UIImage is recognised
Following a book tutorial and seem to run into a snag with the "isGameCenterAvailable" error.
Apparently it is undeclared. Everything else seems to work however so I just need to figure this part out.
Helloworldlayer .m init method
#import <GameKit/GameKit.h>
GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper]; gkHelper.delegate = self;
[gkHelper authenticateLocalPlayer];
Class gameKitLocalPlayerClass = NSClassFromString(#"GKLocalPlayer"); bool isLocalPlayerAvailable = (gameKitLocalPlayerClass != nil);
// Test if device is running iOS 4.1 or higher
NSString* reqSysVer = #"4.1";
NSString* currSysVer = [[UIDevice currentDevice] systemVersion]; bool isOSVer41 = ([currSysVer compare:reqSysVer
options:NSNumericSearch] != NSOrderedAscending);
isGameCenterAvailable = (isLocalPlayerAvailable && isOSVer41);
-(void) onLocalPlayerAuthenticationChanged {
[delegate onLocalPlayerAuthenticationChanged];
}
-(void) authenticateLocalPlayer {
GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
if (localPlayer.authenticated == NO) {
[localPlayer authenticateWithCompletionHandler: ^(NSError* error) {
[self setLastError:error]; }];
}
}
Gamekit.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#protocol GameKitHelperProtocol
-(void) onLocalPlayerAuthenticationChanged; -(void) onFriendListReceived: (NSArray*)friends; -(void) onPlayerInfoReceived:(NSArray*)players; #end
#interface GameKitHelper : NSObject {
id<GameKitHelperProtocol> delegate; bool isGameCenterAvailable; NSError* lastError;
}
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
#property (nonatomic, readonly) bool isGameCenterAvailable; #property (nonatomic, readonly) NSError* lastError;
+(GameKitHelper*) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) getLocalPlayerFriends;
-(void) getPlayerInfo:(NSArray*)players;
#end
helloworld layer.h
#import "GameKitHelper.h"
#interface helloworldlayer : CCLayer <GameKitHelperProtocol>
{
}
gamekithelper. h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#protocol GameKitHelperProtocol
-(void) onLocalPlayerAuthenticationChanged; -(void) onFriendListReceived:(NSArray*)friends; - (void) onPlayerInfoReceived:(NSArray*)players; #end
#interface GameKitHelper : NSObject {
id<GameKitHelperProtocol> delegate; bool isGameCenterAvailable; NSError* lastError;
}
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
#property (nonatomic, readonly) bool isGameCenterAvailable; #property (nonatomic, readonly) NSError* lastError;
+(GameKitHelper*) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) getLocalPlayerFriends;
-(void) getPlayerInfo:(NSArray*)players;
#end
The problem is you never actually declare isGameCenterAvailable. To fix this, do this:
//HelloWorldLayer.h
#property (nonatomic) BOOL isGameCenterAvailable;
//HelloWorldLayer.m
#synthesize isGameCenterAvailable = _isGameCenterAvailable;
UPDATE:
To fix the delegate error, try this:
//HelloWorldLayer.h
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
//HelloWorldLayer.m
#synthesize delegate;
Hope this helps!
How to access variables of other class? This is how I implemented it.
#interface Class1 :NSObject {
NSString *Data;
}
#property (nonatomic, retain) NSString *Data;
#implementation Class1
#synthesize Data;
someMethod{
self.Data = #"something";
}
and in Class2 :
#implementation Class2
someMethodOfClass2{
Class1 *c=[[Class1 alloc]init];
[c someMethod];
NSString *str=c.Data;
}
I get c.Data as null in Class2. Am I doing anything wrong here?
-----------myClass1--------------
#interface APIManager : NSObject {
NSString *Data;
}
#property (nonatomic, retain) NSString *Data;
-(void)getData;
#end
#implementation APIManager
#synthesize Data;
-(void)getData{
self.Data=#"response";
}
--------myClass2-------------
#interface Search : NSObject {
}
-(void)searchForItems:(NSString *)query;
#end
#implementation Search
-(void)searchForItems:(NSString *)query {
APIManager *apiManager=[[APIManager alloc]init];
[apiManager getData];
NSLog(#"%#",[apiManager Data]);
}
You should probably use self.Data = #"something" instead of self.Data = "something"
In Objective-C you have to use #"something" instead of "something". Also aren't you missing the variable declaration? In your #interface you should do something like NSString *Data.
I'm having some trouble figuring out to call methods that I have in other classes
#import "myNewClass.h"
#import "MainViewController.h"
#implementation MainViewController
#synthesize txtUsername;
#synthesize txtPassword;
#synthesize lblUserMessage;
- (IBAction)calculateSecret {
NSString *usec = [self calculateSecretForUser:txtUsername.text
withPassword:txtPassword.text];
[lblUserMessage setText:usec];
[usec release];
}
...
myNewClass.h
#import <Foundation/Foundation.h>
#interface myNewClass : NSObject {
}
- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd;
#end
myNewClass.m
#import "myNewClass.h"
#implementation myNewClass
- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd
{
NSString *a = [[NSString alloc] initWithFormat:#"%# -> %#", user, pwd];
return a;
}
#end
the method CalculateSecretForUser always says
'MainViewController' may not respond to '-calculateSecretForUser:withPassword:'
what am I doing wrong here?
The keyword "self" means the instance of your current class. So you are sending the message calculateSecretForUser:withPassword to MainViewController which does not implements it. You should instantiate myNewClass and call it :
- (IBAction)calculateSecret {
myNewClass *calculator = [[myNewClass alloc] init];
NSString *usec = [calculator calculateSecretForUser:txtUsername.text
withPassword:txtPassword.text];
[lblUserMessage setText:usec];
[usec release];
[calculator release];
}