Xcode 6: Why this code doesn't compile now? - ios

My code was compiling and running great until I upgraded to Xcode 6.
Definition shows a Warning : Auto property synthesis will not synthesize property 'hash' because it is 'readwrite' but it will be synthesized 'readonly' via another property
#property (nonatomic, strong) NSString *hash; // (get/compute) hash code of the place (master hash of images)
Implementation shows error whenever I access to _hash: Use of undeclared identifier '_hash'
-(NSString *)hash {
if (_hash) return _hash;
// If place id, take it as the hash code
NSString *poiID = self.info[#"id"];
if (poiID) {
_hash = [NSString stringWithFormat:#"id-%lu",(unsigned long)[self.address hash]];
}
else if (CLLocationCoordinate2DIsValid(self.location.coordinate)) {
NSString *seed = [NSString stringWithFormat:#"%f,%f", self.location.coordinate.latitude, self.location.coordinate.longitude];
_hash = [NSString stringWithFormat:#"location-%lu",(unsigned long)[seed hash]];
}
else if (self.address) {
NSString *seed = self.address;
_hash = [NSString stringWithFormat:#"address-%lu",(unsigned long)[seed hash]];
}
else {
_hash = #"POI-unknownIDLocationOrAddress";
}
return _hash;
}

It doesn't compile because hash is already part of NSObject:
See:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html#//apple_ref/occ/intfm/NSObject/hash

You need to add the following line to auto generate the setter method:
#synthesize hash = _hash;
If you don't want a setter method and only want a read-only property:
#property (nonatomic, strong, readonly) NSString *hash;

Related

NSString : leak when assigning value to a property

Assuming we don't use ARC.
Suppose we have a very simple class in which we declare 2 NSString properties, like this :
#interface Foo : UIView {}
-(id)initWithArguments:(NSString*)mess title:(NSString*)tit;
#property(nonatomic, retain) NSString *message;
#property(nonatomic, retain) NSString *title;
#end
and in implementation :
#implementation Foo
#synthesize message, title;
-(id)initWithArguments:(NSString*)mess title:(NSString*)tit{
if((self = [super init])){
message = mess; // (1)
self.title = tit; // (2)
(...)
}
return self;
}
-(void)dealloc{
message = nil;
title = nil;
[super dealloc];
}
#end
Now if I call a method from another class, in which I create 2 NSString and an instance of Foo , like this :
-(void)someMethod{
NSString *string1 = [NSString stringWithFormat:#"some text with %d things", 5];
NSString *string2 = [NSString stringWithFormat:#"other text with %d things", 5];
Foo *foo = [[Foo alloc] initWithArguments:string1 title:string2];
}
The whole code works fine and doesn't crash, but, if I profile it with instruments,
it doesn't cause a leak when calling (1)("message = mess;")
it cause a leak when calling (2)("self.title = tit;")
It's very confusing, because stringWithFormat is an autoreleased object, isn't it ?
So, how an autoreleased object can cause a leak when assigning to a property ???
I read somewhere that it's almost always better to use the "self.text = value;" form instead of the "text = value;" form, because the second one may cause a leak.
Actually, in this code it's the contrary.
And... If I use a constant NSString like #"some text", instead of the values returned by [NSString stringWithFormat], there is no leak, of course.
Any idea ?
You have forgotten to invoke the (compiler-generated) setter methods in a few cases:
self.message = mess; // in init method
self.message = nil; // in dealloc method
self.title = nil; // ditto
It's crucial that you use the setter/getter methods in non-ARC code.

isEqualToString always returns False

A little background here before I get started, basically we are looking to compare a UDP response with a string stored in Parse's database for our app. This issue is that I can't seem to get the strings to be considered equal by the isEqualToString function. Here's the code I have running now, I have tried a few work-arounds I've seen in other questions but it still doesn't work.
- (BOOL) onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
if(tag == TAG_SINGLE_GRILL)
{
NSString *grillId = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(grillId.length > 11)
{
grillId = [grillId substringToIndex:11];
}
grillId = [NSString stringWithFormat:#"%#", grillId];
if([grillId hasPrefix:#"GMG"])
{
for(int i = 0; i < [parseGrills count]; i++)
{
NSString *parseGrillId = [[parseGrills objectAtIndex:i] grillId];
parseGrillId = [NSString stringWithFormat:#"%#", parseGrillId];
//If we match the id, add it to found grills
if([grillId isEqualToString:parseGrillId])
{
//do stuff
}
}
}
NSLog(#"Grill ID : %#", grillId);
}
return TRUE;
}
parseGrills is an NSMutableArray with a very basic Grill object, I use synthesize for the properties, otherwise the .m file is essentially empty.
#import <Foundation/Foundation.h>
#interface Grill : NSObject
#property (nonatomic) NSString* grillId;
#property (nonatomic) NSString* ipAddress;
#end
Here's a screen shot of the debugger after it returns false
Any help would be greatly appreciated. Thanks.
I guess that they are of different encoding.
I have run this experiment and see that if the encoding is different, it will return NO. So, try converting parseGrillId to utf8 with the code below.
NSString *s1 = [NSString stringWithCString:"HELLO123" encoding:NSUTF8StringEncoding];
NSString *s2 = [NSString stringWithCString:"HELLO123" encoding:NSUTF16StringEncoding];
NSString *s3 = [NSString stringWithUTF8String:s2.UTF8String];
if ([s1 isEqualToString:s2]) {
NSLog(#"s1 == s2");
}
if ([s1 isEqualToString:s3]) {
NSLog(#"s1 == s3");
}
Will print s1 == s3.

detect the class of a property with name in objective-c [duplicate]

This question already has answers here:
property type or class using reflection
(2 answers)
Closed 9 years ago.
I have an NSObject in objective-c at runtime and i want to know the class of a property in this object , i have the name of this property as NSString , how can I do that.
EDIT :
IntrospectionUtility class :
#implementation IntrospectionUtility
// this function returns an array of names of properties
+ (NSMutableArray*) getProperties:(Class)class
{
NSMutableArray *properties = [[NSMutableArray alloc] init];
unsigned int outCount, i;
objc_property_t *objc_properties = class_copyPropertyList(class, &outCount);
for(i = 0; i < outCount; i++) {
objc_property_t property = objc_properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]];
[properties addObject:propertyName];
}
}
free(objc_properties);
return properties;
}
#end
class test :
#interface JustAnExample : NSObject
#property (nonatomic, strong) NSString *a;
#property (nonatomic, strong) NSString *b;
#property (nonatomic, strong) NSString *c;
#property (nonatomic, strong) NSString *d;
#end
#implementation JustAnExample
- (void) justAnExampleTest
{
NSMutableArray *attributes = [IntrospectionUtility getProperties:self.class];
for (NSString *attribute in attributes) {
//i want to know the type of each attributte
}
}
#end
i have the name of this property as NSString
You can use the function class_getProperty(Class cls, const char *name) to find the property for a given class. Then use property_getAttributes(objc_property_t property) to get the property's attributes, including the encoded type string. Read the Declared Properties section of the Objective-C Runtime Programming Guide for more info.

Usage of the "copy" property attribute to maintain an immutable NSString

I am very new to iOS development and programming in Objective-C. I have been doing the exercises on the app dev library.
This is the current exercise that I am trying to understand.
3. Test what happens if you set a mutable string as the person’s first name, then mutate that string before calling your modified sayHello method. Change the NSString property declarations by adding the copy attribute and test again.
I attempt to do this however, the NSString that I modify does in fact change despite the use of the copy property attribute.
Here are my declarations and implementations as well as my test code.
XYZPerson.h
#import <Foundation/Foundation.h>
#interface XYZPerson : NSObject
#property (copy) NSString *firstName;
#property NSString *lastName;
#property NSDate *dob;
- (void)sayHello;
- (void)saySomething:(NSString *)greeting;
+ (id)init;
+ (id)personWithFirstName:(NSString *)firstName lastName:(NSString *)lastName dob:(NSDate *)dateOfBirth;
#end
//XYZPerson.m
#import "XYZPerson.h"
#implementation XYZPerson
#synthesize firstName = _firstName;
#synthesize lastName = _lastName;
#synthesize dob = _dob;
- (void)sayHello {
[self saySomething:#"Hello World!"];
NSLog(#"This is %# %#", self.firstName, self.lastName);
}
- (void)saySomething:(NSString *)greeting {
NSLog(#"%#", greeting);
}
+ (id)init {
return [self personWithFirstName:#"Yorick" lastName:#"Robinson" dob:8/23/1990];
}
+ (id)personWithFirstName:(NSString *)firstName lastName:(NSString *)lastName dob:(NSDate *)dateOfBirth{
XYZPerson *person = [[self alloc] init];
person.firstName = firstName;
person.lastName = lastName;
person.dob = dateOfBirth;
return person;
}
#end
//Test code
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "XYZPerson.h"
#import "XYZShoutingPerson.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
XYZPerson *guy = [XYZPerson init];
[guy sayHello];
//I thought that this change would never be made, but it is everytime I run the code.
guy.firstName = #"Darryl";
[guy sayHello];
XYZShoutingPerson *girl = [XYZShoutingPerson init];
[girl sayHello];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Consider this shorter example (which runs in CodeRunner btw):
#import <Foundation/Foundation.h>
#interface Person : NSObject
#property (nonatomic,strong) NSString *name; // strong should be copy
#end
#implementation Person
#end
int main(int argc, char *argv[]) {
#autoreleasepool {
Person *p = [Person new];
NSMutableString *name = [[NSMutableString alloc] initWithString:#"Alice"];
p.name = name;
NSLog(#"%#",p.name); // prints Alice
[name appendString:#"xxx"];
NSLog(#"%#",p.name); // prints Alicexxx
}
}
I'm pointing the name to a mutable string, then appending some characters. As a result, the name has changed inside the object. However, if you replace strong with copy when declaring the property, a new immutable string will be created just for the Person object.
The moral of the story is, using copy prevents side effects when someone passes an object and then that object changes.
The message -[NSString copy] results in a copy when a mutable string is passed (NSMutableString) or retain when it is immutable (NSString). Therefore, always copy when declaring NSString properties:
#property (nonatomic,copy) NSString *string; // OK
#property (nonatomic,strong) NSString *string; // strong should be copy
I ran into this problem when I was doing the same book. I added copy and the exact same thing happened, it kept mutating when I appending something to the NSMutableString variable that I used for firstName. Then I read this section:
If you need to set a copy property’s instance variable directly, for example in an initializer method, don’t forget to set a copy of the original object:
-(id)initWithSomeOriginalString:(NSString *)aString {
self = [super init];
if (self) {
_instanceVariableForCopyProperty = [aString copy];
}
return self;
}
So, I went back into my XYZPerson.m and looked at my init code.
I changed:
- (id)initWithFirstName:(NSMutableString *)aFirstName lastName:(NSString *)aLastName
dateOfBirth:(NSDate *)aDate {
self = [super init];
if (self) {
_firstName = aFirstName;
_lastName = aLastName;
_dateOfBirth = aDate;
}
return self;
}
To:
- (id)initWithFirstName:(NSMutableString *)aFirstName lastName:(NSString *)aLastName
dateOfBirth:(NSDate *)aDate {
self = [super init];
if (self) {
_firstName = [aFirstName copy];
_lastName = aLastName;
_dateOfBirth = aDate;
}
return self;
}
And presto-chango: it worked the correct way! It made a copy of the NSMutableString that I had used that did not mutate when I appended something to the end of it before the method call.
I think you are misunderstanding of what copy does.
NSMutableString *string = [NSMutableString stringWithString:#"test"];
XYZPerson *guy = [XYZPerson init];
guy.firstName = string;
guy.lastName = string;
[string replaceCharactersInRange:NSMakeRange(1, 1) withString:#"x"];
[guy sayHello];
Output
This is test txst
In this example, firstName is copy do it doesn't change when string is changed, lastName is not copy so it value is changed when the mutable string string is changed.
What happened here is lastName and string are the same object, so when string is changed lastName is changed as a side effect. This is considered very bad and you never want this behavior. Using copy makes sure firstName and string are different objects and changes to string cannot effect firstName.

How to localize text based on criterion other than language

I have an application which will be marketed in different European countries. We've gone through the process of localizing the application so that its strings are maintained in the language-specific .lproj files in the Settings.bundle. This all works fine. The problem is that there are some strings which don't key off language, but off the country where the app is run. For example, there are strings which differ between the Austrian version of the app and the German version of the app, even though both these countries speak German. When it's run for the first time, the app asks the user which country it's running in.
Is there a way in which I can maintain these country-specific strings in a resource file, and have the resource file used at run time be decided by a user setting, in this case the country where the app is running, rather than the device language?
Thanks,
Peter Hornby
Define two bundles on a singleton, fallback and preferred...
#import <Foundation/Foundation.h>
#interface Localization : NSObject
#property (nonatomic, retain) NSString* fallbackCountry;
#property (nonatomic, retain) NSString* preferredCountry;
#property (nonatomic, retain) NSDictionary* fallbackCountryBundle;
#property (nonatomic, retain) NSDictionary* preferredCountryBundle;
+(Localization *)sharedInstance;
- (NSString*) countryStringForKey:(NSString*)key;
#end
#import "Localization.h"
#implementation Localization
#synthesize fallbackCountryBundle, preferredCountryBundle;
#synthesize fallbackCountry, preferredCountry;
+(Localization *)sharedInstance
{
static dispatch_once_t pred;
static Localization *shared = nil;
dispatch_once(&pred, ^{
shared = [[Localization alloc] init];
[shared setFallbackCountry:#"country-ES"];
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
[shared setPreferredCountry:[NSString stringWithFormat:#"country-%#",countryCode]];
});
return shared;
}
-(void) setFallbackCountry:(NSString*)country
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:#"strings"];
self.fallbackCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
trace(#"Fallback: %# %#",[bundlePath lastPathComponent], self.fallbackCountryBundle);
}
-(void) setPreferredCountry:(NSString*)country
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:#"strings"];
self.preferredCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:nil];
if (!exists) warn(#"%#.strings %#", country, exists ? #"FOUND" : #"NOT FOUND");
trace(#"Preferred: %# %#",[bundlePath lastPathComponent], self.preferredCountryBundle);
}
- (NSString*) countryStringForKey:(NSString*)key
{
NSString* result = nil;
if (preferredCountryBundle!=nil) result = [preferredCountryBundle objectForKey:key];
if (result == nil) result = [fallbackCountryBundle objectForKey:key];
if (result == nil) result = key;
return result;
}
#end
Then call it from a macro function
#define countryString(key) [[Localization sharedInstance]countryStringForKey:key];
Write a default file for ES, and one file per supported language. eg:
/*
country-ES.strings
*/
"hello" = "hello";
And just get the value for the key:
countryString(#"hello");

Resources