One thing that I like about readwrite properties is that you get KVO compliance 'for free', so I tend to use it on properties even when they are only written to from within the object to which the property belongs. On the other hand, I understand that a property should only be set to readwrite if it is intended to be writeable by other objects. So, should I use readwrite even though I only call the setter from self:
[self setFoo:bar];
The alternative (I think) is to use:
[self willChangeValueForKey:#"foo"];
foo = bar;
[self didChangeValueForKey:#"foo"];
which is an extra two lines I code I have to write every time I want to change foo. Which is better?
You can declare a property readonly in the public interface, then promote it to readwrite in a class extension in the implementation file.
Foo.h:
#interface Foo: NSObject
#property (readonly) NSString *frob;
#end
Foo.m:
#interface Foo ()
#property (readwrite) NSString *frob;
#end
#implementation Foo
#synthesize frob;
// Methods in Foo.m can now use foo.frob = #"whatever";
#end
in .h
#property(nonatomic,readwrite,retain)NSString *foo;
then
in .m
#synthesize foo;
then use anywhere like
self.foo=#"madhu";
or
self.foo=#"mike";
but if u synthesized like above then u have to use always like
self with dot
everytime while change the string
it will automatically release the older object then retain the new one.so no pain to take care of old one for release and no pain for retain the new one.
i think its better
Related
Hi I have a relative strange Problem and it costs me a lot of nerves :-)
I have a *.h file with content of
#property (nonatomic, retain) NSString *name;
I synthized it also in the *.m file
#synchronized name;
Works great but I have a (void) Function in my code which writes a string into Variable name like:
name = #"Test";
And in an IBAction of a button I would like to use the stored string of name but name is empty in the IBAction part.
in the IBAction
NSlog(#"%#",name);
What are my problems?
Maybe someone can help.
In new Xcode 6.3 I get this warning:
Auto property synthesis will not synthesize property 'homeInt'; it will be implemented by its superclass, use #dynamic to acknowledge intention
How I can remove it?
If you are overriding the same property from the super class on purpose, then in your *.m or *.mm file, add #dynamic like:
#implementation MyClass
#dynamic homeInt;
// ...
#end
If not, rename the property.
I simply removed this property declaration, because it has already been declared in parent class
Following on #mplace's comment, in my case I was overriding the property to refine the type of the property to a subclass of the original class of the property. So, I did need the #property override.
Here's what I'm using:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-property-synthesis"
// superclass type for currentValue was "id"
#property (nonatomic, strong) NSDate *currentValue;
#pragma clang diagnostic pop
Note that it's "-Wobjc-property-synthesis" and not "-Wno-objc-property-synthesis"
See also https://github.com/couchbase/couchbase-lite-ios/issues/660
If you want to avoid adding #dynamic <varName> each place that you have overridden a super class's property intentionally, you can add the -Wno-objc-property-synthesis flag to "Other Warning Flags" under your projects build settings. This will suppress the warning project-wide.
this cause by child class define the same property name override to parent class,such as:
1)child class "AFHTTPSessionManager" have define :
#property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * **responseSerializer**;
2)parent class "AFURLSessionManager" have define:
#property (nonatomic, strong) id <AFURLResponseSerialization> **responseSerializer**;
3)cause by above, warning come! if want remove it ,just rename the conflict property name!
4) or as it suggest, add "#dynamic homeInt" in your implement file;
If you updated to Xcode 6.3, simply update AFNetworking to version 2.5.2 and these warnings should disappear.
#synthesize homeInt = _ homeInt;
...
#end
Trying to do something really simple, but can't figure out the syntax.
I have a class called Word.h which has 8 properties, strings and integers. For the sake of keeping things simple, I'll stick to 2 here:
#import <UIKit/UIKit.h>
#interface Word : NSObject
#property (nonatomic, strong) NSString *word;
#property (nonatomic, strong) NSNumber *wordLevel;
#end
Both properties are synthesised in the .m file
I then want to create some objects in another file (UIViewController). In the .h file I have this:
#import "Word.h"
and in the .m file, this:
Word *newWord = [[Word alloc] init];
[newWord setWord:#"theorise"];
[newWord setWordLevel:6];
Word *newWord1 = [[Word alloc] init];
[newWord setWord:#"implicit"];
[newWord setWordLevel:7];
Word *newWord2 = [[Word alloc] init];
[newWord setWord:#"incredible"];
[newWord setWordLevel:9];
I now get an error message "Implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC"
What am I doing wrong...is the property defined incorrectly in the class file?? How do I access this property. It works fine with the string.
I will also want to access the properties later - how do I do that...for example:
cell.label1.text = [newWord2 wordLevel];
Is this the right syntax???
Hoping someone can help me, tearing clumps of hair out here!
M
You declared wordLevel to be an NSNumber, an object. You are treating it in your code like it is a plain C int. You have to decide which your want it to be and treat it that way consistently. For example, for a plain C int property you would instead declare:
#property (nonatomic, assign) int wordLevel;
On the other hand if you really want wordLevel to be an NSNumber you need to use the setter like this:
[newWord setWordLevel:[NSNumber numberWithInt:6]];
I am making an iPhone application that goes through a set of viewcontrollers that collect data from user input. At the end of the views I encapsulate all of the data in an customized object called GELObject. Now I need to save this data in a NSMutableArray somewhere so that it can be accessed by a tableviewcontroller that is another branch off of the rootviewcontroller. I was thinking a global variable from the tableviewcontroller, but I did some research and I am reading about singleton's in the appdelegate. Some guidance would be greatly appreciated and if you're feeling extra generous a quick explanation of how to make and use singleton's because it is intriguing me.
Thanks!
To manage a singleton you create a global variable (can be restricted to one file's scope with static) with an initial sentinel value of nil, and use a class method to create the singleton on the first call.
For example:
static Something* globalSomething = nil;
#implementation Something
+ (id)
sharedSomething
{
if (! globalSomething)
{
/* can use different initializer if necessary */
globalSomething = [[[self class] allocWithZone:NULL] init];
}
return globalSomething;
}
. . .
#end
I ran 'analyze" in xcode on a current iOS project to try to track down a freeze issue and there are a number of memory alerts that I don't understand (screenshot of one below).
What is going on there: I have a custom ObjC class extending NSObject; in the init method I alloc/init an NSMutableArray and then in a loop, populate it with NSMutableArrays. This nested array is declared as a property and released in dealloc(). It lives for the life of the app.
Am I doing this wrong? I don't understand the alert#3: # object not referenced in this execution path and has a retain count of +1.
Since my class allocs the outer array, it owns it and will clean it up. Do the inner arrays need to be released?
Thanks for any tips - still new at this.
EDIT/ADDITION
Trying to stamp out the additional memory warnings I am getting so I thought I would add to the question here in the event someone stumbles upon this w/ the same issue.
I am getting the following alert with the code below (the 2nd line "[keyArray addObject: etc"). What is going on: I have a custom class (Key - based on NSObject) that I instance and store in an array. Based on answers to my previous question, I guess my alloc increases the retain count and then when it is added to the array, the retain count isn't decremented - so the memory warning occurs.
What is the proper way to handle something like this? Use a placeholder like this:
Key * k = [[Key alloc] initKeyWithPath:path isBlackKey:NO]];
[keyArray addObject: k];
[k release];
Is that the proper way to do it? Or is there I way to write the custom class to return an autoreleased obj? (thanks and sorry to be so long winded!).
Potential leak of an object allocated on line 460
Method returns an Objective-C object with a +1 retain count (owning reference)
Object allocated on line 460 is not referenced later in this execution path and has a retain count of +1 (object leaked)
-(void) addOctaveToArraysWithTransform:(CGAffineTransform*)trans andPath: (CGMutablePathRef) path
{
path = [self createCF_keyUsingTransform: trans];
[keyArray addObject:[[Key alloc] initKeyWithPath:path isBlackKey:NO]];
}
Key.h
#import <Foundation/Foundation.h>
#import "Key.h"
#interface Key : NSObject {
#public
CGMutablePathRef keyPath;
BOOL isBlackKey;
NSValue * path;
int keyState;
BOOL needsRedraw;
}
#property (nonatomic, assign) int keyState;
#property (nonatomic, assign) BOOL needsRedraw;
#property (nonatomic) CGMutablePathRef keyPath;
-(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag;
-(CGMutablePathRef) getKeyPath;
#end
Key.m
#import "Key.h"
#implementation Key
#synthesize keyState, needsRedraw, keyPath;
-(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag
{
if ((self = [super init])){
isBlackKey = flag;
keyState = 0;
needsRedraw = NO;
keyPath = aPath;
CGPathRetain(keyPath);
}
return self;
}
-(CGMutablePathRef) getKeyPath
{
return keyPath;
}
#end
Yes, you have to release the inner arrays to balance the alloc/init. Remember the outer array will retain each inner array, and the outer array will presumably release those later. But here you are still responsible for the alloc/init you just did.
Hope that helps.
You have an allocation of an NSMutableArray on each iteration of the for-loop. Instead use: NSMutableArray array] which is a convenience method that return an autoreleased NSMUtableArray suitable for adding to fieldNotes which will retain the NSMutableArray.