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.
Related
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];
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]];
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
I'm a student using Xcode 3.2 and What I need to know is how the user can type input and then I can use that input in a formula. I.e. the user inputs 5 and in another field 7 and then I use both numbers in a math formula to return a value
I'm new to C and C++ (I use java), and I have been searching the web for an answer and I somehow can't find what I'm looking for although it's a relatively simple concept. Code examples are definitely preferred and greatly appreciated.
Ok the best way to do this is to convert the numbers inputed into floats then do whatever math problem you want and then convert the answer into a string and display the string in a label. As you can see here in my .h file:
IBOutlet UITextField *rectWidth;
IBOutlet UITextField *rectLength;
IBOutlet UILabel *rectResult;
}
#property (retain, nonatomic) UITextField *rectWidth;
#property (retain, nonatomic) UITextField *rectLength;
#property (retain, nonatomic) UILabel *rectResult;
-(IBAction)calculate:(id)sender;
As you can see I have 2 text fields for the user to input the data. Right now I was using it for an area and volume finder but you can name them whatever you want. Also I have the calculate action to tell the program what to do when the button is clicked. Then in your .m
-(IBAction)calculate:(id)sender {
float floatRectResult=[rectWidth.text floatValue]*
[rectLength.text floatValue];
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%1.2f",floatRectResult];
rectResult.text=stringRectResult;
[stringRectResult release];
}
So all you need to do is put this code in and change the * behind float floatRectResult=[rectWidth.text floatValue] to whatever formula you want to use. Like addition, subtraction division etc. So in interface builder connect the text fields to the two text fields and the label to the label on screen. Then create a button and link it to the IBAction calculate. And that should be all. Hope this helps.
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.