UILabel.text == self.NSString? [duplicate] - objective-c

This question already has answers here:
Compare two NSStrings
(3 answers)
Closed 9 years ago.
My question may be simple for some to answer, but I'm relatively new to Objective C and Xcode. So I have a UILabel and I am running an if statement asking if UILabel is equal to self.NSString then do ... Here is the code.
if (UILabel.text == self.NSString)
{
//Do Something here...
}
I'm wondering if this would work, or what I have to do in order for this to start working.
Thanks in advance.

Use isEqualToString: method from NSString class.
if([text isEqualToString:string])
{
// Do something here.
}

When comparing strings, you should use
[UILabel.text isEqualToString:self.NSString]
The == simply compares the pointers, which will often be different even if their contents are the same. The isEqualToString method compares the contents.

Related

Is Checking The Pointer Of An Object With Negation Operator The Same As With Comparing It To Nil? [duplicate]

This question already has answers here:
Testing for nil in Objective-C -- if(x != nil) vs if(x)
(4 answers)
Closed 6 years ago.
I maybe naming it wrong, so please correct me.
I just want to know if this
if(!obj)
{
//do something
}
is exactly the same as this
if(obj == nil)
{
}
I mean are they both checking whether the object points to nil?
Yes, this is exactly the same. ! operator turns anything other than nil into zero, while nil becomes 1. Therefore, you get exactly the same results as the comparison with nil.
Similarly, using if (obj) is logically identical to if (obj != nil). This convention is inherited from C, where pointers are commonly used in conditions, and NULL-checked with ! operator.
Yes, it's the same. That's right. Yep.

Objective C expression not understood [duplicate]

This question already has answers here:
Weird objective-c syntax - square brackets and # sign
(2 answers)
Closed 8 years ago.
I come from a C++ background and am learning Objective-C.
One expression that I've encountered is not clear for me. It is as follows:
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
What I don't understand is "#[indexPath]". Why do i need [] and #?
The method is...
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:...
and takes an array as the parameter (i.e. multiple index paths).
The code...
#[indexPath]
uses objective-c literals to create an array.
The equivalent in "old" code is...
[NSArray arrayWithObjects:indexPath, nil];

How to implement methods found in the Mac Developer Library [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm a totally new to Objective C programming and just need to check how would I implement something I find on the Mac Developer Library.
For example I found this code in the Mac Developer Library:
- (BOOL)containsObject:(id)anObject
Are the steps I need to implement this method?
in the .h file add
-(BOOL)containsObject:(id)anObject;
then in the .m file implement it like this;
- (BOOL)containsObject:(id)anObject
{
//What do I place here to search my array with the objects?
return YES;
}
Can someone help me with an example how I would search an array of numbers and use this method?
This is my array:
NSArray *first;
first = [NSArray arrayWithObjects:1,2,3,4,5, nil];
Do you mean "implement" - as in write your own version - or "use"? You seem to start with the former and end with the latter...
How do you use it? Well taking your example, corrected:
NSArray *first = [NSArray arrayWithObjects:#1, #2, #3, #4,# 5, nil];
The #1 etc. is a shorthand to create an NSNumber object - you can only put object references and not primitive values into an NSArray. The shorthand effectively expands to [NSNumber numberWithInt:1] etc.
Now to use containsObject::
BOOL containsTheAnswerToEverything = [first containsObject:#42];
This will set the BOOL variable to YES if the array contains an NSNumber representing 42, and NO otherwise.
How do you implement it? You examine each element of your array in a loop comparing each to the object you are looking for, same algorithm you would use in C (which it seems you know).
If you are asking how to define a class, then you should read up on Objective-C, but your point 2 is the correct outline.
HTH
In respect to the first question, you can do just that. An example to give you a better visual understanding would be this:
YourClassName.h
#import <Foundation/Foundation.h>
#interface YourClassName : Subclass //replace subclass with something like NSObject or whatever you need
{
}
- (BOOL)containsObject:(id)anObject;
#end
and...
YourClassName.m
#import "YourClassName.h"
#implementation YourClassName
- (BOOL)containsObject:(id)anObject
{
//Insert function body here
}
#end
As for your second question, I am not really very familiar with using NSArray or loading it using that strange function. My advice would be using (NSArray)anObject instead of (id)anObject as you can load the array directly into your function and create your search parameters there. I'm not sure exactly what object you are looking for in terms of containsObject. Are you seeing if it contains the number? If the value contains the array? specify it a bit and I may be able to dig up a better answer for you
EDIT:
It occurred to me that you are probably just looking for the number inside of the array seeing as you are newer to Objective-C. To accomplish what you want you have a couple options. Both require the change of your function. The first would be just changing the function to this:
- (BOOL)containsObject:(int)object fromArray:(int*)array length:(int)length;
In YourClassName.h. Now you can always change your parameters to different data types but this will work for your integers. You can also do this without the length parameter but I figure it saves us some code (personal preference). And in the .m file:
- (BOOL)containsObject;(int)object fromArray:(int*)array length:(int)length
{
for(int i = 0; i <= length; i++)
{
if (array[i] == object)
{
return YES;
}
}
return NO;
}
second would be just without the length option and a bit more code

Combining two strings for a label's text [duplicate]

This question already has answers here:
Setting a cell's text with multiple JSON objects
(3 answers)
Closed 9 years ago.
Real beginners question here. I'm doing some tutorials and I'm expanding on them with scenarios that might come in to play.
Anyhow, I know that the following works:
lblCopyStatus.text = #"Test";
But how would do:
lblCopyStatus.text = #"Test" & lbltest.text;
e.g. get the text from another label into the text. I hope that makes sense :)
For example:
lblCopyStatus.text = [NSString stringWithFormat:#"Test %#", lbltest.text];

When is if (!someObject) (short for someObject != nil) NOT safe? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
is there a difference between (!object) and (object == nil)?
In iOS (Objective C) development, I've frequently seen (and used) the following short hand:
if (someObject)
{
// do something
}
To check that someObject is not nil.
In other words, to mean the same as the following:
if (someObject != nil)
{
// do something
}
Are these two if statements actually the same or is this not safe?
The two are exactly the same. They both are equally safe.
This feature is inherited from C, where comparing to zero is implicit.