How to compare self.title to a string in Objective C? - iphone

What am I doing wrong in the below code? My if statement is missing something:
if ([self.title] = "Upcoming Events") {
} else {
}

Correct would be:
if( [self.title isEqualToString:#"Upcoming Events"] )
self.title is a pointer, you can only compare it to other pointers using == not their values.

if ([self.title isEqualToString:#"Upcoming Events"]) {
NSLog(#"True");
} else {
NSLog(#"False");
}

You just have to write like this:
if([self.title isEqualToString:#"Upcoming Events"])
{
}
else
{
}

also .... in a if you should use "==" instead of "=". When there is "==" it is checking if they are equal while if there is "=" it gives the first one the value of the second.

Related

flutter - if then else - variable made local only

As you can see in the code below, I want to check if a string has a ? in his name.
If yes, I remove it.
My problem is that the variable 'nameOfFileOnlyCleaned' stay local and is empty after the if else.
Thank you for your help.
String nameOfFile = list_attachments_Of_Reference[j].toString().split('/').last;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
String nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
String nameOfFileOnlyCleaned = nameOfFile;
}
//Here my variable 'nameOfFileOnlyCleaned' is empty.
This is a problem because the value should be used later
in the code. Do you know why I have this issue please?
Many thanks.
String extensionFile = nameOfFileOnlyCleaned.split('.').last;
String url_Of_File = list_attachments_Of_Reference[j].toString();
you should define your variable before if/else statement as follows:
String nameOfFileOnlyCleaned = "";
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
for example:
String nameOfFile = "test?test";
String nameOfFileOnlyCleaned;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
print(nameOfFileOnlyCleaned);
it returns: test as a result.

Iterate over part of String in Swift

Why in the world are Swift String operations so complex and tiresome to work with?
I have to iterate over a String in reverse but ignoring the first char. Now this could be done like following:
var firstTime = true
for i in textBefore.characters.reversed() {
if firstTime {
firstTime = false
} else {
if String(i).personalFunction() {
// something
} else {
// something else
}
}
}
But really I just want to do something like:
textBefore = textBefore.characters.reversed()
for i in 1...textBefore.characters.count {
if textBefore.get(i).personalFunction() {
// something
} else {
// something else
}
}
So why can't we get index as int. And why is textBefore.characters.reversed() not a String or simply have String have a reverse function. All these issues just makes it so frustrating to work with Strings in Swift and makes us do stupid stuff as converting a String to an array of chars :S or stuff like my proposed solution above... Also we can't make for loops in the old fashion... I simply need some Swift guru to point my brain in the right direction for this stuff.
string.characters is a collection of characters.
Use reversed() to access the elements in reverse order, anddropFirst() to skip the initial element of the reversed collection:
let string = "a🇨🇷b😈"
for ch in string.characters.reversed().dropFirst() {
print(ch)
// `ch` is a Character. Use `String(ch)` if you need a String.
}
Output:
b
🇨🇷
a
You can do something like your second one. After you enter the for, you can just get the index directly from the string. In Swift, a string is just an array of characters.
textBefore = String(textBefore.characters.reversed())
for i in 1...textBefore.characters.count {
if textBefore[i].personalFunction() {
// something
} else {
// something else
}
}

How to compare NSString having " sign?

I am trying to do like this, but it gives error as " is present.
if (!([post.videoID isEqualToString:#"<null>"])) {
How can I compare such string which contains " with it?
This is valid way to check null
if (!([post.videoID isEqual:[NSNull null]]))
{
}
And this is your solution...
if (!([post.videoID isEqualToString:#"\"<null>\""]))
{
}
Always remember to put \ as escape sequence when there is a " available in the code.
if (!([post.videoID isEqualToString:#"\"<null>\""])) {
You will have to check with [NSNull null] object.
if (!([post.videoID isEqual:[NSNull null]])) {
}
NSString *p=#"\"";
if ([p isEqualToString:#"\""]) {
NSLog(#"YES");
}
TRy this
NSString *str=[NSString stringWithFormat:#"\"%#\"",post.videoID ];
if (!([str isEqualToString:#"\"<null>\""])) {}

Get the text in string variable

I m confused with this from many days and couldn't understand how to resolve it.
I get some data from web server and assigning it to string variable.In assigning it if sometimes no data is available then that string is updated to null(NULL) and to nil(nil) sometimes to (null).So I m confused how to compare data in that variable.
if(stringvariable==NULL) // couldnot understand how to compare here ,with NULL or nil or (null)
{
// do something
}
When will the string variable change its state (to NULL or nil or (null)) ?
use this code..
if([stringvariable isEqualToString:#""] || [stringvariable isEqual:nil])
{
//Data not Found
}
else{
// Data not nil
}
You can check like
if([str length]>0 || ![str isEqualToString:#""]) {
// String is not empty
}
It should be :
if(![stringvariable isEqualToString:#""])
{
// stringvariable is not Empty.
}
else
{
// stringvariable is Empty.
}

Weird String Comparison Error

The "currentPasswordBox" is a UITextField.
if (currentPasswordBox.text == #"1234") {
NSLog(#"Correct");
}
else {
NSLog(#"Incorrect");
}
}
The Log says (when I type 1234 into the UITextField) "Incorrect"
What am I doing wrong?
== will do a pointer compare, which is not correct. You want
if ([currentPasswordBox.text isEqualToString:#"1234"]) {