Objective C Singleton class "defined but not used" warning! - iphone

I'm using a Singleton class and following is the code:
.h File: 
#import <Foundation/Foundation.h>
#interface Credential : NSObject {
NSString *UID;
NSString *UPASS;
}
#property(nonatomic,retain) NSString *UID;
#property(nonatomic,retain) NSString *UPASS;
static Credential *credential = NULL;
+(Credential*) sharedInstance;
/*
+ #property(nonatomic,retain) NSString *UID;
+ #property(nonatomic,retain) NSString *UPASS;
*/
#end
.m file:
#import "Credential.h"
#implementation Credential
#synthesize UID,UPASS;
-(void) dealloc{
[UID release];
[UPASS release];
[super dealloc];
}
+(Credential*) sharedInstance
{
#synchronized(self)
{
if (credential == NULL) {
credential = [[Credential alloc] init];
}
}
return credential;
}
#end
The following line produces warning "defined but not used"
static Credential *credential = NULL;
I couldn't figure out that I've been using credential variable in .m file under "sharedInstance" function then why am I getting this warning?
A strange issue to me!

Does the problem go away when you move the static variable to the top of the implementation (.m) file? And on a related note, I think that you would benefit from getting rid of the singleton altogether.

Related

Accessing changeable values in a singleton?

First off, I come from Lua, don't blame me for being global variable minded lol. So, I've been reading up on how to use this whole "Singleton system" and I'm not sure if I'm completely missing the point or if I'm just implementing it incorrectly?
The goal of my code is to create a way for multiple files to access a variable that holds the size of an array in a specific file. Here is my singleton:
.h
#import <Foundation/Foundation.h>
#interface GlobalVariables : NSObject
{
NSNumber *currentGameArrayCount;
BOOL *isGamePaused;
}
#property (nonatomic, readwrite) NSNumber *currentGameArrayCount;
#property (nonatomic, readwrite) BOOL *isGamePaused;
+ (GlobalVariables *)sharedInstance;
#end
.m
#import "GlobalVariables.h"
#implementation GlobalVariables
#synthesize currentGameArrayCount, isGamePaused;
static GlobalVariables *gVariable;
+ (GlobalVariables *)sharedInstance
{
if (gVariable == nil) {
gVariable = [[super allocWithZone:NULL] init];
}
return gVariable;
}
- (id)init
{
self = [super init];
if (self)
{
currentGameArrayCount = [[NSNumber alloc] initWithInt:0];
isGamePaused = NO;
}
return self;
}
#end
and in another file with the array I use:
GlobalVariables *sharedData = [GlobalVariables sharedInstance];
NSNumber *tmpArrayCount = [sharedData currentGameArrayCount];
NSInteger tmpCount = [whereStuffActuallyHappens.subviews count]; // Subviews is the array
NSNumber *currentCount = [NSNumber numberWithInteger:tmpCount];
tmpArrayCount = currentCount;
the hope of this code was to get the variable in the singeton (currentGameArrayCount) and set it too what the current array count was (currentCount). Am I incorrectly interpreting the purpose of a singleton? Am I just bad at singletons and didn't set it up correctly? Does anyone know how I could achieve the result of getting my array count to be accesible to all my files?
You have a few issues. Try these changes:
GlobalVariables.h:
#import <Foundation/Foundation.h>
#interface GlobalVariables : NSObject
#property (nonatomic, assign) int currentGameArrayCount;
#property (nonatomic, assign) BOOL gamePaused;
+ (GlobalVariables *)sharedInstance;
#end
GlobalVariables.m:
#import "GlobalVariables.h"
static GlobalVariables *gVariable = nil;
#implementation GlobalVariables
+ (GlobalVariables *)sharedInstance {
if (gVariable == nil) {
gVariable = [[self alloc] init];
}
return gVariable;
}
- (id)init {
self = [super init];
if (self) {
self.currentGameArrayCount = 0;
self.gamePaused = NO;
}
return self;
}
#end
Now in your other code you can do:
GlobalVariables *sharedData = [GlobalVariables sharedInstance];
int tmpArrayCount = sharedData.currentGameArrayCount;
NSInteger tmpCount = [whereStuffActuallyHappens.subviews count]; // Subviews is the array
sharedData.currentGameArrayCount = tmpCount;

init my own api class

I have an API class, lets call it MyClass, which I want to be available thruout my whole application. Hence I put the #import into MyProject-Prefix.pch file.
Now, when I (in my appDelegate) initiate the class, I get the error message cannot init a class object..
I understand that I am initiating my class incorrectly, but I have no idea on how I should do it... and since this is not common for development (as I see it) there ain't a lot of information to find thrue google (or I am searching wrong ;) )
So, I have two questions:
Any links for called "Creating Objective-C API's for Dummys"? ;)
Having a quick look at my code, what am I doing wrong?
This is my class:
MyClass.h
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import "NSStringAdditions.h"
#import "XMLRPCResponse.h"
#import "XMLRPCRequest.h"
#import "XMLRPCConnection.h"
/**
* END required libs
*/
extern int EID;
extern NSString * SHARED_SECRET;
extern NSString * MODE;
#interface MyClass : NSObject
+ (id)initWithEid:(int)eid secret:(NSString*)secret mode:(NSString*)mode;
+ (NSArray*)web_invoice_infoWithOCR:(NSString*)ocr pno:(NSString*)pno;
+ (NSString*)the_digest:(NSString*)source;
#end
MyClass.m
#import "MyClass.h"
#implementation MyClass
int EID;
NSString * SHARED_SECRET;
NSString * MODE;
NSString * URL_LIVE;
NSString * URL_BETA;
#pragma mark init / dealloc
+ (id)init {
self = [super init];
if (self)
{
}
return self;
}
+ (id)initWithEid:(int)eid secret:(NSString*)secret mode:(NSString*)mode
{
self = [super init];
if (self)
{
if (![[mode lowercaseString] isEqualToString:#"beta"] && ![[mode lowercaseString] isEqualToString:#"live"])
{
#throw ([NSException exceptionWithName:#"Invalid mode" reason:[NSString stringWithFormat:#"Invalid mode '%#' selected. Should either be 'beta' or 'live'", mode] userInfo:nil]);
}
EID = eid;
SHARED_SECRET = secret;
MODE = [mode lowercaseString];
}
return self;
}
+ (NSArray*)web_invoice_infoWithOCR:(NSString*)ocr pno:(NSString*)pno {
NSArray *params = [NSArray arrayWithObjects:ocr, EID, pno, [MyClass the_digest:[NSString stringWithFormat:#"%d:%#:%#", EID, ocr, SHARED_SECRET]], nil];
NSLog(#"Array: %#", params);
return params;
}
- (id)xmrpc_call_function:(NSString*)method parameters:(NSArray*)params
{
// Not finished yet, Warning exists
}
[...]
Having a look at my code, you'll notice the +(id)init function. I have tried -(id)init, -(id)initialize, +(id)initialize, +(void)initialize, -(void)initialize.
This is how I "load" my class in my AppDelegate:
[MyClass initWithEid:1234 secret:#"1234" mode:#"BETA"];
EDIT
I am trying to initiate my class the same way e.g. Flurry does. Example:
[FlurryAnalytics startSession:#"1234"];
You need to alloc it first:
MyClass *myClass = [[MyClass alloc] initWithEid:1234 secret:#"1234" mode:#"BETA"];

invalid CFArrayRef problem with Singleton object

I've built a singleton object to manage some data in my app
#interface MyCommon : NSObject {
NSArray *quizz;
int iCurrentQuestion;
};
+ (MyCommon *)singleton;
#property (retain) NSArray *quizz;
#property (assign) int iCurrentQuestion;
#end
MyCommon.m
#import "MyCommon.h"
// MyCommon.m:
#implementation MyCommon
static MyCommon * MyCommon_Singleton = nil;
#synthesize iCurrentQuestion;
+ (MyCommon *)singleton
{
if (nil == MyCommon_Singleton)
{
MyCommon_Singleton = [[MyCommon alloc] init];
NSLog(#"allocating MyCommon_Singleton at %#",MyCommon_Singleton);
}
else {
NSLog(#"accessing singleton : %#", MyCommon_Singleton);
}
return MyCommon_Singleton;
}
- (NSArray*) getQuizz{
return quizz;
}
- (void) setQuizz:(NSArray *)array {
quizz = [NSArray arrayWithArray:array];
NSLog(#"setQuizz : %#",quizz);
}
There is no problem for writing the quizz object (setQuizz), however when I try to access it for reading, I get a crash : the quizz looks invalid and Xcode notify me an invalid CFArrayRef
I don't know what's wrong with my code.
You provide a custom setter for quizz but it doesn't comply with how the property is declared.
You're not retaining quizz when you're setting a new value. It's likely to be released just after, leading to a crash when you access it.
You should write
- (void)setQuizz:(NSArray *)array {
if (quizz != array) {
NSArray *tmp = quizz;
quizz = [array retain]; // retain the new value
[tmp release]; // release the old one
}
NSLog(#"setQuizz : %#",quizz);
}
this is way more code than it needs to be. First if you are going to be providing your own method you should declare so in the #property declaration which you didn't. Also your not properly retaining your variables. Additionally you should be using dispatch_once() for a thread safe & fast way to guarantee the singleton is only created once.
#interface MyCommon : NSObject {}
#property(nonatomic, retain) NSArray *quiz;
#property (assign) int iCurrentQuestion;
+ (MyCommon *)singleton;
#end
#implementation MyCommon
#synthesize quiz;
#synthesize iCurrentQuestion;
-(id)init {
self = [super init];
if(self) {
quiz = [[NSMutableArray alloc init];
iCurrentQuestion = 0;
}
return self;
}
+ (MyCommon *)singleton {
static MyCommon *singleton = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
singleton = [[MyCommon alloc] init];
});
return singleton;
}
#end
then you just do
[MyCommon singleton].quiz = //some array

passing NSString from one class to the other

I have a NSString that is taken from a UITextField in a ViewController. Every of my other ViewController will use this NSString as well. How can I pass this NSString to others ViewControllers?
You want to have a property in each of your controllers
#interface MyViewController : UIViewController{
NSString *title;
}
#property (retain) NSString *title;
#end;
#implementation MyViewController
#synthesize title;
#end;
Use it like:
MyViewController *myVC = [[MyViewController alloc] initWithFrame:...];
myVC.title = #"hello world";
You should be familiar with Memory Management
Create a class for sharing your common objects. Retrieve it using a static method, then read and write to its properties.
#interface Store : NSObject {
NSString* myString;
}
#property (nonatomic, retain) NSString* myString;
+ (Store *) sharedStore;
#end
and
#implementation Store
#synthesize myString;
static Store *sharedStore = nil;
// Store* myStore = [Store sharedStore];
+ (Store *) sharedStore {
#synchronized(self){
if (sharedStore == nil){
sharedStore = [[self alloc] init];
}
}
return sharedStore;
}
// your init method if you need one
#end
in other words, write:
Store* myStore = [Store sharedStore];
myStore.myString = #"myValue";
and read (in another view controller):
Store* myStore = [Store sharedStore];
myTextField.text = myStore.myString;
If the string remains the same, and never changes, you could make a file named defines.h (without the .m file) and have this line:
#define kMyString #"Some text"
Then wherever you need the string, just import the defines file and use the constant.
#import "defines.h"
Much simpler than making custom classes.
EDIT:
Didn't see you needed to grab from the text field.
In that case, you could have it stored as property of your app delegate class and get it from there. The delegate can be accessed from anywhere in your app.

EXC_BAD_ACCESS when calling class init in Objective C

I've been trying to figure this out but I can't figure out what I'm doing wrong.
I wrote a class and whenever I try to initialize it, I get a EXC_BAD_ACCESS error. I can't even step into the initialization.
Anyone have any idea what I'm doing wrong?
User *myUser = [myUser init];
.h file:
#import <Foundation/Foundation.h>
#interface User : NSObject {
long rowId;
NSString *email;
NSString *password;
NSString *fileVersion;
}
#property long rowId;
#property (assign) NSString *email;
#property (assign) NSString *password;
#property (assign) NSString *fileVersion;
#end
.m file
#import "User.h"
#implementation User
#synthesize rowId, email, password, fileVersion;
-(id)init {
self = [super init];
return self;
}
#end
You have to actually allocate the object:
User *myUser = [[User alloc] init];
Don't forget to release it when you're done using it.