I am writing an iPhone app and I want to create a NSCache singleton.
I am having trouble, here's the code that I have:
MyAppCache.h:
#import <Foundation/Foundation.h>
#interface MyAppCache : NSCache {}
+ (MyAppCache *) sharedCache;
#end
MyAppCache.m:
#import "SpotmoCache.h"
static MyAppCache *sharedMyAppCache = nil;
#implementation MyAppCache
+ (MyAppCache *) sharedCache {
if (sharedMyAppCache == nil) {
sharedMyAppCache = [[super allocWithZone:NULL] init];
}
return sharedMyAppCache;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedCache] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release{
//do nothing
}
- (id)autorelease {
return self;
}
#end
When I want to add something or get something from the cache I might write:
#import "MyAppCache.h"
MyAppCache *theCache = [MyAppCache sharedCache];
Then:
NSData *someData = [[theCache objectForKey: keyString] retain];
Or:
[theCache setObject: someData forKey: keyString cost: sizeof(someData)];
The problem: the compiler complains 'MyAppCache' may not respond to 'method' for each of those lines.
I might be doing something completely wrong here - know how to make this work?
If the first listing is MyAppCache.h, then you're sticking #implementation in a header file, which isn't likely to do the right thing (the linker will probably complain).
If the first listing is MyAppCache.m, then you need to move the #interface into MyAppCache.h.
Also note that your code suffers from double-initing: [[MyAppCache alloc] init] is effectively [[[MyAppCache sharedCache] retain] init]. I don't know what NSCache does when inited twice, but it probably isn't good. I really wouldn't bother implementing copyWithZone: (I'm pretty sure objects aren't copyable by default), and you can just override allocWithZone: to raise an exception.
(And +sharedCache isn't thread-safe, which may or may not be an issue.)
OK, got it. I had a copy (previous version) of the MyAppCache.h and MyAppCache.m files still sitting in a folder in the project!
Related
I'm getting the following warnings and I haven't got a clue where to start, any ideas?
NSObject+Be.m:36:3: [rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately
NSObject+Be.m:35:40: [rewriter] NSInvocation's getReturnValue is not safe to be used with an object with ownership other than __unsafe_unretained
NSObject+Be.m:36:4: ARC forbids explicit message send of 'autorelease'
The code came from here....
https://github.com/tylerneylon/moriarty/blob/c0d6daf65d86c22b8e5853aef00980f059c92fbc/NSObject%2BBe.m
#import "NSObject+Be.h"
#interface BeProxy : NSProxy {
id target;
}
+ (BeProxy *)beProxyForClass:(Class)class;
- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
#end
#implementation BeProxy
+ (BeProxy *)beProxyForClass:(Class)class {
BeProxy *beProxy = [BeProxy alloc];
beProxy->target = [class alloc];
return beProxy;
}
//
// We assume the method called is an init method. The return value
// may be a new value for self.
- (void)forwardInvocation:(NSInvocation *)anInvocation {
[anInvocation setTarget:target];
[anInvocation invoke];
id object;
[anInvocation getReturnValue:(void *)&object]; //HERE
[object autorelease]; //HERE
[self release];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
return [target methodSignatureForSelector:aSelector];
}
#end
//
#implementation NSObject (Be)
+ (id)be {
return [BeProxy beProxyForClass:[self class]];
}
+ (id)beInit {
return [[[self class] new] autorelease];
}
- (id)beCopy {
return [[self copy] autorelease];
}
#end
From the documentation:
NSObject+Be This category is designed to help with memory management.
Specifically, this makes it easy to only work with autoreleased
objects outside of a small number of ownership-allowed methods.
So the posted code is to help with manual retain count memory management. Don't compile it with ARC, then.
i am working on a project in wich i want to use Singleton Pattern model.
i want to any data model of my this project fallow Singleton Pattern.
i study the apple documentation regarding this
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6
and
http://www.oodesign.com/singleton-pattern.html
http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/
now i know my custom object classes should fallow the main rule of allocing a object but the i need the complete implementation like using of this class object
i am new in iphone app development so if i am wrong in any place in this Question please guide
Try this:
#implementation Singleton
+ (Singleton *)sharedInstance
{
static Singleton *obj = nil;
if (obj == nil)
obj = [[self alloc] init];
return obj;
}
#end
static MyClass *_sharedInstance;
+ (MyClass *)sharedMyClass
{
#synchronized([MyClass class]) {
if (_sharedInstance == nil)
[[self alloc] init];
return _sharedInstance;
}
return nil;
}
+(id) alloc
{
#synchronized([MyClass class]) {
NSAssert(_sharedInstance == nil, #"Attempted to allocate a second instance of MyClass.");
_sharedInstance = [super alloc];
return _sharedInstance;
}
return nil;
}
+ (id) allocWithZone:(NSZone *)zone
{
#synchronized([MyClass class]) {
NSAssert(_sharedInstance == nil, #"Attempted to allocate a second instance of MyClass.");
_sharedInstance= [super allocWithZone:zone];
return _sharedInstance;
}
return nil; //on subsequent allocation attempts return nil
}
- (id) copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax;
}
- (oneway void)release
{
// Do nothing
}
- (id)autorelease
{
return self;
}
If you can target iOS 4 or above, I will take the following way:
//.h
+(MySingletonClass *)mySharedInstance;
-(void)doSomething;
//.m
+(MySingletonClass *)mySharedInstance {
static dispatch_once_t pred;
static MySingletonClass *shared = nil;
dispatch_once(&pred, ^{
shared = [[MySingletonClass alloc] init];
});
return shared;
}
-(void)doSomething
{
}
// override also the init if you want
To access it, do an #import MySingletonClass.h and use it wherever you want like the following:
MySingletonClass* mySharedInstance = [MySingletonClass mySharedInstance];
[mySharedInstance doSomething];
I want to any data model of my this project fallow Singleton Pattern.
Based on my experience, I would not abuse on singletons. The application could become difficult to maintain. To avoid this, put the data models within your singleton. You can access data model directly (creating properties around them) or using public methods (like for example doSomething) as wrappers.
Hope this helps.
This might be a useful reference: http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html
Typically I create the object in the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method and have it live in that object. I make it available to the rest of the app with a macro:
#define APPLICATION ((AppDelegate*)([UIApplication sharedApplication].delegate))
as a readonly property of the app delegate
The benefit of this type of approach if you are in to mocking is that it is just another object property rather than a hidden static object.
I use:
#import <Foundation/Foundation.h>
#interface iCode_Framework : NSObject
#property (readonly, nonatomic) unsigned int iBufCapacity;
#property (readonly, nonatomic) unsigned int iPort;
#property (readonly, nonatomic) NSString * urlStr;
#end
#import "iCode_Framework.h"
static iCode_Framework * instance;
#implementation iCode_Framework
#dynamic iBufCapacity;
#dynamic iPort;
#dynamic urlStr;
- (unsigned int)iBufCapacity
{
return 1024u;
};
- (unsigned int)iPort
{
return 1978u;
};
- (NSString *)urlStr
{
return #"localhost";
};
+ (void)initialize
{
if (!instance) {
instance = [[super allocWithZone:NULL] init];
}
}
+ (id)allocWithZone:(NSZone * const)notUsed
{
return instance;
}
#end
Which is used exactly like a normal class, you call alloc and init! It is often convenient to assign to a variable to give a shorthand, because alloc and init are long to type, e.g.:
#import "iCode_FrameworkTests.h"
#import "iCode_Framework.h"
static iCode_Framework * c;
#implementation iCode_FrameworkTests
+ (void)initialize
{
c = [[iCode_Framework alloc] init];
}
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)testSingletonNotNil
{
STAssertNotNil(c, nil);
}
- (void)testSingletonProperty
{
STAssertEqualObjects(c, [iCode_Framework alloc], nil);
}
- (void)testIBufCapacity
{
STAssertEquals(c.iBufCapacity, 1024u, nil);
}
#end
The advantage of this approach is it is used exactly like any other class and can therefore be mocked for testing.
I want to have one object that is initialized in the delegate and I want to be able to use this object anywhere across view controllers (doesn't depend on what view I am currently at). I am guessing the solution to this would be to have a singleton class, so far I have the following:
#interface LocationManager : NSObject <CLLocationManagerDelegate>{
NSDate *enter;
NSDate *exit;
CLLocationManager * manager;
}
#property (nonatomic, retain) NSDate * enter;
#property (nonatomic, retain) NSDate * exit;
- (BOOL)registerRegionWithLatitude:(double)latitude andLongitude:(double)longitude;
+ (LocationManager *)instance;
#end
#import "LocationManager.h"
#implementation LocationManager
#synthesize enter;
#synthesize exit;
#pragma mark - CLLocationManager delegate
static LocationManager *gInstance = NULL;
+ (LocationManager *)instance
{
#synchronized(self)
{
if (gInstance == NULL)
gInstance = [[self alloc] init];
}
return(gInstance);
}
#end
Is this correct? So all I need to do to access this is just to call instance? Inside LocationManager I also want to have only one CLLocationManager, called manager.. however, where do I initialize it so I only have one? Can I do the following? Most other singleton examples doesn't have any variables in the class, so that's where I got confused
+ (LocationManager *)sharedLocationManager
{
#synchronized(self)
{
if (lm == NULL){
lm = [[self alloc] init];
lm.manager = [[CLLocationManager alloc] init];
lm.manager.delegate = lm;
}
}
return(lm);
}
Basically -- yes.
Just a couple of small things:
static LocationManager *gInstance = NULL;
instead of NULL, you should use nil, it's a convention in Objective-C.
You should also overwrite alloc, new, copyWithZone:, and mutableCopyWithZone:. From Buck/Yacktman: "Cocoa Design Patterns", p. 153:
+ (id)hiddenAlloc
{
return [super alloc];
}
+ (id)new
{
return [self alloc];
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedInstance] retain];
}
- (id)copyWithZone:(NSZone *)zone
{
[self retain];
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return [self copyWithZone:zone];
}
This way, your singleton object cannot be copied. You need to call hiddenAlloc from your instance method (by the way, the method to access a Singleton object is often called sharedInstance in Objective-C).
For other singleton styles with their pros and cons, check out this question.
Personally, I prefer this style (copied from one of the answers on that link):
static MySingleton *sharedSingleton;
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
sharedSingleton = [[MySingleton alloc] init];
}
}
In fact, there's a tried-and-true method to create singletons already. Download the SynthesizeSingleton.h file (from a Cocoa with Love article). It contains a massive amount of pre-processor code which will generate any singleton for you. Hit the article for more details.
Since the factory method "instance" is a class-level method, the #synchronized block should be
#synchronized([LocationManager class]) {
//}
Hi I got some problem with NSOperation .
I always got error at self = [super init]; (already use break point to find this)
it always return "Program received signal: EXC_BAD_ACCESS" all the time
//AddThread.h
#interface AddThread : NSOperation
{
NSString * str;
}
#property (nonatomic,retain) NSString * str;
-(id) initWithString:(NSString *) tmpStr;
#end
and for .m
//AddThread.m
#import "AddThread.h"
#implementation AddThread
#synthesize str;
- (id) initWithString:(NSString *)tmpStr
{
self = [super init];
if (self != nil)
{
self.str = tmpStr;
}
//NSLog(self);
//[super init];
return self;
}
- (void) main
{
NSLog(self.str);
}
- (void) dealloc{
[str release];
str = nil;
[super dealloc];
}
#end
well I stuck with this for while and if possible any resources ,articles things for basic example of NSoperation?
In your main method, you are calling NSLog(self.str) - While this will work if the object you pass in is a string, it won't work if you continue to try and log other objects. NSLog takes a format string as a parameter. If you just do NSLog(self) like you are in some of your commented code, and self is not a string, it'll crash because it expected a string. You should do NSLog(#"self: %#", self) the %# will print out the string returned by an objects description method.
Other than that, your init method looks fine, how exactly are you creating an instance of this object? Could you show the code for that? The problem may lie there.
Before I roll my own Queue using NSMutableArray, I'd like to know if there is something more standard available. I don't see anything in the Apple docs, but I'll be surprised if there is not a Queue implementation from somewhere that folks are using. Java spoils me!
Implementing a queue based on NSMutableArray is pretty easy, it's probably under 50 lines of code.
EDIT:
Found this with a quick google search:
#interface Queue:NSObject {
NSMutableArray* objects;
}
- (void)addObject:(id)object;
- (id)takeObject;
#end
#implementation Queue
- (id)init {
if ((self = [super init])) {
objects = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[objects release];
[super dealloc];
}
- (void)addObject:(id)object {
[objects addObject:object];
}
- (id)takeObject {
id object = nil;
if ([objects count] > 0) {
object = [[[objects objectAtIndex:0] retain] autorelease];
[objects removeObjectAtIndex:0];
}
return object;
}
#end
Cocoa itself doesn't have a Queue class, and there's not a standard per se, but there are several options, one of which may best fit your needs. See this question (and my answer).
Like you said, you can roll your own using NSMutableArray. If you just need a quick'n'dirty queue (and aren't worried about copying, encoding/decoding, enumeration, etc.) then the solution #Matt suggests is an easy approach. You should also consider adding queue methods to NSMutableArray via a category, which is nice in that your "queue" is also an array (so you can pass it for NSArray parameters), and you get all the NS(Mutable)Array functionality for free.
If performance is important, I recommend using a structure more ideally suited for removing the first element. I wrote CHCircularBufferQueue for my own framework for this very reason. (Not trying to toot my own horn, just trying to save others some time.)
I have made a category containing just the deque method, based on Matt Bridges code.
#interface NSMutableArray (ShiftExtension)
// returns the first element of self and removes it
-(id)shift;
#end
#implementation NSMutableArray (ShiftExtension)
-(id)shift {
if([self count] < 1) return nil;
id obj = [[[self objectAtIndex:0] retain] autorelease];
[self removeObjectAtIndex:0];
return obj;
}
#end
You can use the STL queue from the C++ Standard Library.
Check out the STL priority queue. It requires zero lines of code and it's portable! What more could you want?
You could use the :lastObject method of NSArray. Here is an untested example:
Queue.h
#import <Foundation/Foundation.h>
#interface Queue : NSObject
-(void)enqueue:(id)object;
-(id)dequeue;
#end
Queue.m
#import "Queue.h"
#interface Queue()
#property(nonatomic, strong) NSMutableArray *backingArray;
#end
#implementation Queue
-(id)init {
self = [super init];
if (self) {
self.backingArray = [NSMutableArray array];
}
return self;
}
-(void)enqueue:(id<NSObject>)object {
[self.backingArray addObject:object];
}
-(id)dequeue {
id object = [self.backingArray lastObject];
[self.backingArray removeObject:object];
return object;
}
#end