system.getInfo if-statement not working

I’m having a bit of trouble understanding this chunk of code. The result is system.getInfo is always “iPhone” even when I’m using an Android device as the simulator and the “model” value equals “droid” for instance. Does anyone know why that would be the case?

–Setup the back button
if system.getInfo(“model”) == “iPad” or “iPhone” then
print(“iPad or iPhone”)
backBtn = ui.newButton{
default = “backButton.png”,
over = “backButton_over.png”,
onRelease = backBtnRelease
}
else
print(“Not iOS”)
backBtn = ui.newButton{
default = “androidbackButton_over.png”,
over = “androidbackButton.png”,
onRelease = backBtnRelease
}
end
[import]uid: 150151 topic_id: 33513 reply_id: 333513[/import]

Hi there,

I think the issue is in your first if statement, where you are using ‘or’ in a way that might look right in written English but doesn’t work in programming. Try changing that line to [lua]if system.getInfo(“model”) == “iPad” or system.getInfo(“model”) == “iPhone” then[/lua]. Let me know if that helps.

  • Andrew [import]uid: 109711 topic_id: 33513 reply_id: 133153[/import]

Hi there,

I think the issue is in your first if statement, where you are using ‘or’ in a way that might look right in written English but doesn’t work in programming. Try changing that line to [lua]if system.getInfo(“model”) == “iPad” or system.getInfo(“model”) == “iPhone” then[/lua]. Let me know if that helps.

  • Andrew [import]uid: 109711 topic_id: 33513 reply_id: 133154[/import]

Expanding on @aukStudios answer:

In programming the parameters to an IF statement have an order of precedence. Lets rewrite that statement using parens () to group things in the order that they happen:

if ( system.getInfo("model") == "iPad" ) or ( "iPhone" ) then  

This is going to conduct two tests:

  1. Is system.getInfo(“model”) equal to “iPad”? If true, then the “then” clause is executed.
  2. Is “iPhone” true or false? In Lua like most languages, if something has a value (not nil and not false) then it’s true. This will always evaluate to “true”.
  3. Since you did an “or” operator, if either 1 or 2 is true, then the “then” clause will be what’s executed. Since the 2nd one is always true, it will always execute the “then” clause and not the “else” clause.

If you want a simpler test to see if you’re on an Apple product, I do this:

if string.sub(system.getInfo("model"), 1, 2) == "iP" then  

This takes the model name and extracts the first 2 letters and sees if its “iP”, which gets iPhone, iPad and iPod.
[import]uid: 199310 topic_id: 33513 reply_id: 133216[/import]

Gentlmen,

Thank you for your responses! Both soultions worked.

Rob, I didn’t understand your third point. Were you saying that “iPhone” would always evaluate as true because it isn’t nil or false? Wouldn’t it be false on any device that wasn’t “iPhone”? But also, your method of checking encompases iPod as well; I wouldn’t have caught that.

Thank you again, Andrew and Rob!

-Andrew C. [import]uid: 150151 topic_id: 33513 reply_id: 133223[/import]

The last “iPhone” is just a string. Lets forget its contents for a second and just think of it as some string of characters. Is that string nil? Is it a Boolean False? No, it’s not, therefore it by itself evaluates to true.

Had you done this:

if ( system.getInfo("model") == "iPad" ) or ( system.getInfo("model") == "iPhone" ) then  

Then you’re now doing a comparison of getInfo’s return to the string iPhone and asking if they are equal. That itself evaluates to either true or false and would have done what you intended. But in general if you do:

if somevariable then

with no comparison operators, just the variable by itself, it’s asking is that variable false or nil which is false or if its something else, it’s true.
[import]uid: 199310 topic_id: 33513 reply_id: 133225[/import]

Okay, I understand now. Thank you for taking the time to explain it to me. So I was forcing that first line to be true regardless of the contents of the string. [import]uid: 150151 topic_id: 33513 reply_id: 133228[/import]

Hi there,

I think the issue is in your first if statement, where you are using ‘or’ in a way that might look right in written English but doesn’t work in programming. Try changing that line to [lua]if system.getInfo(“model”) == “iPad” or system.getInfo(“model”) == “iPhone” then[/lua]. Let me know if that helps.

  • Andrew [import]uid: 109711 topic_id: 33513 reply_id: 133153[/import]

Hi there,

I think the issue is in your first if statement, where you are using ‘or’ in a way that might look right in written English but doesn’t work in programming. Try changing that line to [lua]if system.getInfo(“model”) == “iPad” or system.getInfo(“model”) == “iPhone” then[/lua]. Let me know if that helps.

  • Andrew [import]uid: 109711 topic_id: 33513 reply_id: 133154[/import]

Expanding on @aukStudios answer:

In programming the parameters to an IF statement have an order of precedence. Lets rewrite that statement using parens () to group things in the order that they happen:

if ( system.getInfo("model") == "iPad" ) or ( "iPhone" ) then  

This is going to conduct two tests:

  1. Is system.getInfo(“model”) equal to “iPad”? If true, then the “then” clause is executed.
  2. Is “iPhone” true or false? In Lua like most languages, if something has a value (not nil and not false) then it’s true. This will always evaluate to “true”.
  3. Since you did an “or” operator, if either 1 or 2 is true, then the “then” clause will be what’s executed. Since the 2nd one is always true, it will always execute the “then” clause and not the “else” clause.

If you want a simpler test to see if you’re on an Apple product, I do this:

if string.sub(system.getInfo("model"), 1, 2) == "iP" then  

This takes the model name and extracts the first 2 letters and sees if its “iP”, which gets iPhone, iPad and iPod.
[import]uid: 199310 topic_id: 33513 reply_id: 133216[/import]

Gentlmen,

Thank you for your responses! Both soultions worked.

Rob, I didn’t understand your third point. Were you saying that “iPhone” would always evaluate as true because it isn’t nil or false? Wouldn’t it be false on any device that wasn’t “iPhone”? But also, your method of checking encompases iPod as well; I wouldn’t have caught that.

Thank you again, Andrew and Rob!

-Andrew C. [import]uid: 150151 topic_id: 33513 reply_id: 133223[/import]

The last “iPhone” is just a string. Lets forget its contents for a second and just think of it as some string of characters. Is that string nil? Is it a Boolean False? No, it’s not, therefore it by itself evaluates to true.

Had you done this:

if ( system.getInfo("model") == "iPad" ) or ( system.getInfo("model") == "iPhone" ) then  

Then you’re now doing a comparison of getInfo’s return to the string iPhone and asking if they are equal. That itself evaluates to either true or false and would have done what you intended. But in general if you do:

if somevariable then

with no comparison operators, just the variable by itself, it’s asking is that variable false or nil which is false or if its something else, it’s true.
[import]uid: 199310 topic_id: 33513 reply_id: 133225[/import]

Okay, I understand now. Thank you for taking the time to explain it to me. So I was forcing that first line to be true regardless of the contents of the string. [import]uid: 150151 topic_id: 33513 reply_id: 133228[/import]