Domanda

I am new to objective-C and cocoa.

In my UIViewController I need to access AppDelegate multiple times in different methods

A. Is calling in every method:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

consumes more performance?

B. I tried to create a global parameter in the UIViewController:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface Login_ViewController : UIViewController<UITextFieldDelegate,UIImagePickerControllerDelegate>{
  AppDelegate *appDelegate;
}

Implementation and usage:

- (void)viewDidLoad
{
     appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];  
     appDelegate.businessId = [businessId integerValue]; 
}

- (BOOL)credentialsValidated
{
     appDelegate.businessId = [[NSUserDefaults standardUserDefaults] integerForKey:BUSINESS_ID];
}

But I get warning (although the code works)

Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (aka 'int'); 

The declaration of businessId in appDelegate is:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property NSInteger *businessId;

and the implementation:

@implementation AppDelegate
@synthesize businessId;
È stato utile?

Soluzione

Remove the * from your declaration in the app delegate:

@property NSInteger (assign) businessId;

NSInteger is a primitive, so you do not need an object pointer.

Altri suggerimenti

A. This won't give any performance penalty unless you call it 100000000 a second. BTW never assume smth - always measure. There is a tool called Time Profiler - use it to find all the bottlenecks.

B. NSInteger is just a typedef to int - it is POD type and not ObjC object, so you can't send messages to it. Use NSInteger instead of NSInteger*.

You can define AppDelegate like this #define DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]; now you can use DELEGATE where you need..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top