xcode 4 dot notation code sense problem - xcode

if i use dot notation in xcode 4 code completion doesn't work for me (pressing ESC):
NSString *s = #"demo";
NSLog(#"%lu", [s length]); //[s <ESC> code completion works fine
NSLog(#"%lu", s.length); //s.<ESC> code completion doesn't work
??

Make sure that the property has a valid #property accessor defined.
// in .h
#property (assign) int length;
// in .m
#synthesize length;
Keep in mind you can have your own accessors and setters, but I think code-sense needs #property to show up the dot notation.

Related

Global Variables Xcode

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.

Realm cannot sort properly for capital letter in Xcode

Realm cannot sort properly for capital letter in Xcode. I've wrote following Realm to sort by according speaker name.
RLMResults *speakers = [[SpeakerDB objectsWhere:condition] sortedResultsUsingProperty:property ascending:YES];
It's correctly sorted when all are small and capital letter. But when those mixed with both small and capital letter, sorting got wrong.
Choun-Ki JOO
Chul Young CHOI
CHAN Wing Kwong << it gone wrong sorted.
CHEE Soon Phaik
Connie LAI
Please let me know how to solve that issue.
Realm doesn't support case-insensitive sorting, currently.
See also... https://github.com/realm/realm-cocoa/issues/2970
If you'd like to get a result sorted when all are a small or a capital letter, you should add a property for sorting. The property stores same value as the speaker name property but it contains all small letters(or all uppercase). Like the following:
#interface SpeakerDB : RLMObject
#property (nonatomic) NSString *name;
#property NSString *acturalName;
#property NSString *lowercaseMame;
#end
#implementation SpeakerDB
+ (NSArray<NSString *> *)ignoredProperties {
return #[#"name"];
}
- (void)setName:(NSString *)name {
_name = name;
self.acturalName = name;
self.lowercaseMame = [name lowercaseString];
}
#end
(To override the setter, defining the name property as ignoredProperty.)
Then you can sort by lowercaseMame.
RLMResults *speakers = [[SpeakerDB objectsWhere:condition]
sortedResultsUsingProperty:#"lowercaseMame" ascending:YES];

Xcode basics: Declare custom class with integer properties and use it in another class

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]];

analyze and memory alerts in xcode

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.

iPhone: Problem with incrementing NSNumber & displaying transparent images (UIImageView)

I have a problem with NSNumber: I don't understand how to increment and decrement it! I've tried int with [NSNumber intValue] and so on, but it didn't work!!!! I just want a page counter which can be converted to an NSNumber at the end.
My second problem is displaying a (partially) transparent image in an UIImageView. It has ever a (white) background.
Thanks for answering,
Le Milonkh
HI there
don't use nsnumbers, use ints. Much easier... (NSNumber is merely a wrapper for storing numbers, whereas int provides the default set of C based mathematical interfaces you are looking for, with relative ease - I'm sure you can do math with NSNumber -> although many people say NSDecimal is better. I say use floats, ints and doubles).
int pagenumber;
for(pagenumber = 0; pagenumber < 5; pagenumber++){
NSLog(#"%i", pagenumber);
}
then if you really need it in an NSNumber then do:
NSNumber *pagenumberns = [NSNumber numberWithInt:pagenumber];
In answer to your second question I have never had that problem, but try doing: [ImageView setOpaque:no] and [ImageView setBackgroundColor:[UIColor clearColor]];
Hope some of that helps.
Thank you for your answer, but I need to declare them globally in my ...ViewController.h & .m files because all my methods use them.
Is is possible to do it like that, or have I to #property and #synthesize the int?
FooViewController.h:
#interface FooViewController : UIViewController {
int *currentPage;
}
-(void)next;
-(void)changePage:(int)page;
FooViewController.m:
#implementation Post_GeheimnisViewController
#synthesize currentPage;
-(void)changePage:(int)page {
//do some stuff here
}
-(void)next {
currentPage++;
[self changePage:currentPage];
}
#end
Thank you for answering!
Le Milonkh

Resources