Expected Identifier error in if statement - objective-c

I am trying to create a class similar to the built in NSDictionary class, but I want to add some extra functionality and make it easier to use. In the .m file I have the following piece of code:
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (![theKey isEqual:#""]) && (![theValue isEqual:#""]){
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
}
}
It gives me an "Expected Identifier" error at the start of the second portion of the if statement after the "&&". Would someone be able to help me with this?
EDIT: The original problem is fixed but now there is a new error at the end of the if statement.
-(void)newEntryWithKey:(NSString *)theKey andValue:(NSString *)theValue{
if (theKey.length && theValue.length) {
[self.keys addObject:theKey];
[self.values addObject:theValue];
self.upperBound++;
}else{
return
} //<-- error here "Expected expression"
}

It should be:
if (![theKey isEqual:#""] && ![theValue isEqual:#""]) {
Though a better check for non-empty strings would be:
if (theKey.length && theValue.length) {
Your original if statement will give incorrect results if either theKey or theValue is nil. My 2nd option works in either case.
Update:
The problem with the updated code is the missing ; after the return statement.

Usually after if you should have one condition in parentheses. You're missing parentheses.

Related

Lua if statement using function in for loop

I'm tyring to make kind of macros by using scripts of WoW itself.
And I heard that the script of WoW is based on Lua.
Though I've used Obj-C ,Swift and C++ for several years, Embarrassingly it was quite hard to write down some code with Lua.(eventhough it was quite short.)
I'm trying to return boolean value by function for if statement in for loops.
If it was Obj-C, it would be like below.
-(void) script {
Bool on = NO;
NSArray* zone = [#"Feralas",#"The hinterlands",#"Duskwood"];
for(int i = 0 ; i < 3 ; i ++) {
if([self clearCheck:i]) {
NSLog(#"stone on! -> %#",zone[i]);
on = YES;
}
}
if (!on) {
NSLog(#"No Stone Today..");
}
}
-(Bool) clearCheck:(int)index {
return [C_QuestLog IsQuestFlaggedCompleted:index+44329];
}
And A Lua code I tried is below
local o,z=false,{"Feralas","The hinterlands","Duskwood"}
function f(x) return C_QuestLog.IsQuestFlaggedCompleted(44329+x) end
for i=1,3 do
if f(i) then do
print("stone on! -> ",z[i])
o=true
end
end
if not o then do
print("no stone today..")
end
What did i miss? and How Should it be?
I hope someone could help me from this difficulty...
You're probably seeing an error message like
'end' expected (to close 'if' at line ...) near
That's because you have a superfluous do in your code that "steals" the end you provided for the if. So you're short one end per if statement.
Remove do after then.
Lua 5.4 Reference Manual: 3.3.4 Control Structures
stat ::= if exp then block {elseif exp then block} [else block] end
Aside from that your code is syntactically correct.

Ternary operator error - "Expected expression"

I haven't used the ternary operator much and I'm getting an error on this:
(isInitializing) ? (return YES) : (isInitializing = 1);
Error is: "Expected Expression" and it's pointing to return YES.
Don't use the Ternary Operator to "do stuff" but to return one of two values.
So this is a valid case:
NSString *something = (isInitializing ? #"value a" : #"value b");
In your case, you might want to do this instead:
if (isInitializing) {
return YES;
} else {
isInitializing = 1;
}
The ternary operator is used to return one of two values depending on a condition. It is not so much used to execute statements, hence the return is a bit of a problem. I would rather use an if when you do not want to distinguish values, but rather have two different execution paths.

error with switch case and exptected expression

Sorry to ask this (I rarely use switch statements) but I am getting an error with this but it seems valid to me (but obviously isn't):
NSInteger section=indexPath.section;
switch(section){
case 0:
Item *mi = self.miArray[indexPath.row]; // <- expected expression
...
return cell;
break;
case 1:
Item *mi = self.miArray[indexPath.row];
break;
}
What am I doing wrong?
You can either put the case in braces (case 0: { Item *mi ... }) or add a ; after the case statement (case 0:;).
Either of that should help but I actually forgot why this is necessary.
Found an explanation here: Weird Switch error in Obj-C
Declaration is not allowed within switch.
Declare Item before entering to switch and do the initialization within the switch.

Finding Sub-String in UITextView

I'm trying to do a check to see if there are any sub-strings in a UITextView. Currently, this is how I go about doing that:
if ([self.textView.text rangeOfString:#"string"].location == NSNotFound)
{
NSLog(#"self.textView.text contains string");
}
Unfortunately, when I try to run this code I don't get a response, indicating that the test failed, as there is in fact the word: string used in the UITextView. How would I go about checking if there are any sub-strings in a UITextView?
Just change != to ==! The if statement is telling you that the instance of string not found. Use this statement instead:
if ([self.textView.text rangeOfString:#"string"].location != NSNotFound)
{
NSLog(#"self.textView.text contains string");
}
Same thing you can implement like this below as well. If you do not want to change the equal operator condition:-
if (![self.textView.text rangeOfString:#"string"].location == NSNotFound)
{
NSLog(#"self.textView.text contains string");
}

When I Use EG(active_symbol_table) I get error

I write a pecl function named sample_isset, and its code is :
PHP_FUNCTION(sample_isset) {
zval **fooval;
if(EG(active_symbol_table) == NULL) {
php_printf("the active symbol table is NULL");
}else if(zend_hash_find(EG(active_symbol_table),"foo",4,
(void **) &fooval) == SUCCESS){
php_printf("Got the value of $foo!");
} else {
php_printf("$foo is not defined!");
}
}
And I want to use this function to see if current symbol table has a $foo variable.
When I use it in global scope, it works well.
But When I use it in another function for example hello, I go a error, and see nothing .
The hello function may be like this.
function hello(){
sample_isset();
}
I don't know why I got an error.
It seems that the php function used the lazy loading, the active_symbol_table doesn't always exist, so You should use zend_rebuild_symbol_table before EG(active_symbol_table) .