I have problem assigning a NSString value to a property of my model class. When I NSLog the string out, it is there, but when I try to assign the string to a property and then log out the property, its always null, could you please help me with that?
The property I am trying to assign a value to is defined like so:
#property (nonatomic, retain) NSString * text;
I am assigning the value like so:
NSString *categoryText = [self.pickerCategoryText objectAtIndex:[self.categoryPickerView selectedRowInComponent:0]];
newFilter.category.text = [NSString stringWithFormat:#"%#", categoryText];
I have also tried to alloc and init the property and also assign the value like so:
newFilter.category.text = categoryText;
but none of these solutions worked. I am now totally lost and without any other possible clues, could you please help me with that ?
Thx
At the time when you perform:
newFilter.category.text = [NSString stringWithFormat:#"%#", categoryText];
... either newFilter or category might be nil. If they are nil nothing gets changed.
Related
I would like to verify something that I always use but when I think about it ... I get confused why it worked that way and I sure I read the explanation about it but I cant find it.
As far as I understand apple create their setter as something like this.
-(void)setString:(NSString *)value {
if (_string != value) {
[_string release];
_string = [value retain];
}
}
Now usually I create properties like this.
#property (nonatomic) NSString *string;
#synthesize string = _string;
The question is about next code:
NSString *s = #"Should be deleted";
[self setString:s];
NSLog(#"string check111 =%#",self.string);
s = NULL;
NSLog(#"string check222=%#",self.string);
The same output will be generated. From the setter I can see that my property points on the object that I changed but the property value will be the same.That situation triggers another question (if it works like that why would I need copy attribute).
Can someone provide a short explanation about it? (or concrete link to read).
Tnx A Lot. (I think my question may be already asked in the forum )
This has no effect because you are changing the object to which s points to.
This diagram probably explains it better, originally you have something like this:
Changing the point of s will not affect _string.
The idea of setting the property to copy is in case you set your string property to a mutable string and then change the content of it. See this question.
I guess it would be something like this
NSString *s = #"Should be deleted"; // create autoreleased string
[self setString:s]; // retain string
NSLog(#"string check111 =%#",self.string);
s = NULL; // reset pointer value to null. This operation doesn't affect string object
NSLog(#"string check222=%#",self.string);
// string's retain counter will be decreased by autorelease pool later
I read several threads about this topic but I can't solve my problem. Please help me. I think I'm too stupid to solve my problem.
My Problem: I have a ViewController and I work with storyboards - I created a NNString property - NSString gets a value - then I wanted to print the NSString with NSLog out but I got always the value null
Thanks for help and sorry for this maybe stupid question. This are little parts of my code, because I think the whole code is to long to post it here.
ViewController.h
#property (nonatomic, retain) NSString *loggedinID;
ViewController.m
#synthesize loggedinID,
...
First method a IBAction
loggedinID = [jsonData objectForKey:#"id"] ;
Now the other method a IBAction, where i want tu use the property
NSLog(#"%#", loggedinID);
Like guys in comment says... First of all get rid of this #synthesize thing. Compiler do this automatically for you when you declare property. From now you should access your property by mutators (setter and getter)
self.loggedinID = [jsonData objectForKey:#"id"]; // Calling setter
[self.loggedinID stringWithString:[jsonData objectForKey:#"id"]]; // Seeter as well
_loggedinID = [jsonData objectForKey:#"id"]; // ivar
[_loggedinID stringWithString:[jsonData objectForKey:#"id"]]; // ivar as well
It's a good way to access your properties via mutators. Only good place, for me, to use ivar are initializers.
Second, check this jsonData... This could be the problem of null in your string property.
Last, at the beginning you've declared #property called loggedinID and later you try to log loggedinFirstName.
At first: you don't need to synthesize anymore.
To answer your question: You are logging a different variable.
loggedinFirstName != loggedinID
Don't use #synthesize in ARC. Try putting value by prefixing self to your string Variable.
Also you can make the string STRONG instead of RETAIN.
Thanks
I have a value which I am pulling from a CFDictionaryRef as you see here:
CFDictionaryRef GPS = (CFDictionaryRef)CFDictionaryGetValue((__bridge CFDictionaryRef)(mutableMetadata), kCGImagePropertyGPSDictionary);
NSDictionary *gps_dict = (__bridge NSDictionary*)GPS;
NSLog(#"gps_dict: %#",gps_dict);
I then pull the latitudeValue from the dictionary values. I have declared latitudeValue as and NSString property in the header file and synthesized it #implementation:
latitudeValue = [gps_dict objectForKey:#"Latitude"];
At this point I would like to take the value for latitudeValue and have it update the text field on the view. The text field is titled latValue and has been connected as an IBOutlet in the header file. I have tried quite a few variations of latValue.text = latitudeValue; but they all cause thread errors when I run the program. What is the best way for me to make this connection? Thanks for your time.
[gps_dict objectForKey:#"Latitude"] is probably a number object (and not a string).
Therefore you should declare latitudeValue as NSNumber property, and then:
latValue.text = [latitudeValue stringValue];
or better
self.latValue.text = [self.latitudeValue stringValue];
using the property accessors. (Note that you don't have to #synthesize properties
explicitly with the current compiler versions.)
I made a property of NSString and initialised it in a function of the class.
Now I am using NSLog() another function , to show what this string contains.
Instead of getting what I entered, I am getting weird string.
My code listing was following:
#property(nonatomic, retain) NSString *stringWithProperty;
Now in implementation:
- (IBAction)FirstButtonPressed:(id)sender
{
NSString *x= [labelForSecondLine.text substringWithRange:NSMakeRange(0, 2)];
NSString *y= [labelForSecondLine.text substringWithRange:NSMakeRange(3, 2)];
NSString *z= [labelForSecondLine.text substringWithRange:NSMakeRange(5, 2)];
stringWithProperty=[NSString stringWithFormat:#"%#-%#-20%#",x,y,z];
}
- (IBAction)secondButtonPressed:(id)sender
{
NSLog(#"%#",stringWithProperty);
}
When I press second button after pressing first, I get this output:
<UIButtonContent: 0x71533b0 Title = (null), AttributedTitle = (null), Image = (null), Background = (null), TitleColor = UIDeviceWhiteColorSpace 1 1, ShadowColor = UIDeviceWhiteColorSpace 0 0.5>
Can anyone tell me whats going wrong with me or with this code?
Properties are weird, until you get used to them. (In fact, they're weird even after you get used to them.)
Complicating matters is the fact that Apple keeps playing with the way properties work.
If you want to be relatively independent of compiler version, and avoid assorted oddities, insert #synthesize stringWithProperty; just below your #implementation statement, and always refer to the property as self.stringWithProperty. In later versions of the compiler these steps are unnecessary, but there is no harm in them.
if there is no #synthesize stringWithProperty=stringWithProperty; for property, underlaying variable name will be set to _stringWithProperty. You're hitting some object with same name (looks like it is some button content). Use self.stringWithProperty to access property or (if you really need to do so) _stringWithProperty to access it's variable directly (or maybe even better more explicit self->_stringWithProperty)
i have a uitextfield, when it is initialized and i didn't input any values into it, i found the value of the uitextfield is not null nor nil.
NSString *notes = (notesField.text)?(notesField.text):#"hello";
NSLog(#"notes: %#",notes);
it returns nothing for notes
NSString *notes1;
//or use legnth
if ([notesField.text isEqual:#""]) {
notes1=#"hello";
NSLog(#"empty textfield: %#",notes1);
//then it returns "hello"
}
else
{
notes1=notesField.text;
NSLog(#"not empty textfield: %#",notes1);
}
Why is that? Can I still user ternary operator ?
like this ?
NSString *notes = ([notesField.text length])?(notesField.text):#"hello";
You can use
NSString *notes = ([notesField.text length])?(notesField.text):#"hello";
OR
NSString *notes = ([notesField.text length]==0)?#"hello":(notesField.text);
OR
NSString *notes = ([notesField.text isEqualToString:#""])?#"hello":(notesField.text);
And for the case when your UITextField has no entry (initial case), use the second or third option , that will be beter. NSString *notes = ([notesField.text length])?#"hello":(notesField.text); won't work fine as you expect because notesField.text will be TRUE even if there is no text in textfield. So you should use notesField.text.length or [notesField.text isEqualToString:#""].
Hope its clear now.
The docs for UITextField state
text
The text displayed by the text field.
#property(nonatomic, copy) NSString *text
Discussion
This string is #"" by default.
Note: if compiling under Xcode 5.02 or 5.1 and running in iOS earlier than iOS7, UITextField.text is initialised to nil. If running in iOS7+ it is initialised to #"".
If compiling in Xcode 4.6.3 or earlier then UITextField.text has (always) been initialised to #"", as per the documentation.
Radar bug: 16336863
UITextField must be initializing itself with a non-nil empty string. In situations where you don't care if the string is empty or nil, you can simply check the length property:
if (!notesField.text.length) {
// text is nil or empty
}
Or using the ternary operator:
NSString *s = notesField.text.length ? notesField.text : #"Default";
This works because sending the -length selector to a nil object will return a default value of 0.
That approach will work just fine.
As for why it's an empty string instead of nil, it's generally bad practice to return nil for something unless you want to indicate an error or an uninitialized state. An empty text field has a value. It's just an empty string.