I have this inside a custom UIActionSheet class
if (otherButtonTitles != nil) {
[self addButtonWithTitle:otherButtonTitles];
va_list args;
va_start(args, otherButtonTitles);
NSString * title = nil;
while(title = va_arg(args,NSString*)) { // error here
[self addButtonWithTitle:title];
}
va_end(args);
}
I have this error
! using the result of an assignment as a condition without parentheses
pointing to this line
while(title = va_arg(args,NSString*)) {
why is that?
thanks.
This is probably not an error as you said, it's a warning.
The compiler is warning you that you should surround assignments within parentheses when it is within a conditional to avoid the ol' assignment-when-you-mean-comparison mistake.
To get past this rather pedantic compiler warning, you can simply surround the assignment within another pair of parentheses:
while((title = va_arg(args,NSString*))) {
//...
}
It should be a warning and not an error. It's trying to warn in case you're using = but you meant ==.
In this case, you're not meaning to use == because you're calling va_arg() multiple times to iterate through otherButtonTitles and assigning it to the temp var title, so just add another set of parentheses. The warning will go away.
while((title = va_arg(args,NSString*))) {
compiler thinks that you're doing assignment by mistake, hence suggesting this error. You should wrap your statement into ONE MORE PAIR of parenthesis like this:
while((title = va_arg(args,NSString*)))
Then compiler first will evaluate the assignment, then the result of that assignment will be passed into the while condition
It does seem a little picky, but have you tried:
while((title = va_arg(args,NSString*)) != NULL)
?
Related
Is there a difference between something like
if let error = error {
print(error.localizedDescription)
}
and just checking if it is = nil
if error != nil {
print(error.localizedDescription)
}
if I want to check if error has a value? Think of firebase's creating user function.
Yes.
The if let statement allows you to bind the value of error to a variable if it is non-nil and use it in the block. If maybeError is of type Error?, when you do:
if let error = maybeError {
/* block contents */
}
the type of error will be Error within the block - ie: It won't be an optional anymore. If you just do a nil-check using if, error will still be of type Error? within the block. So the code that would actually be equivalent to your first snippet would be:
if error != nil {
print(error!.localizedDescription)
}
(Your second snippet, as it is, won't compile, as you're trying to get the localizedDescription variable of an Error? object, which has no such property)
By the way, in case you haven't seen it before, the !. thing is the unwrap operator. It runs the method on the object if the object is non-nil, but it crashes in case the object is nil. In this case, you generally know it won't crash. (But this might not actually be safe depending on where and how you use it - check #rmaddy's comment)
In the first, error is now a non-optional type, so you can use .. This is also idiomatic -- it more clearly shows what you are trying to do.
In the second, you would need to use ?. to look at error's properties (your code won't compile because you haven't done that).
I was wondering what this code does:
var something: String = "Hi"
if something = "Hello world!" {
// Will this be executed?
}
Will it assign to something variable and do the if body? Or will it set the value of that variable only for the if body and outside it will not change? Or has it anything to do with nil?
Assignments are not expressions that return booleans, so cannot be used inside an if like this. So this won’t compile.
(though you will get a misleading compiler message)
This pattern only works for assignments that can fail — that is, if you're assigning the result of an expression that returns an Optional value. And in that case, you use if let, not just if.
we can't use assignment operator for if condition, you would you if let suppose if you are working with optional types
Here are some operators that helps you get clarity to differentiate from assignment operator
= assignment operator
== is equal to
=== is identical to
I'm very new to swift, but proficient in other languages like Java, JavaScript, C, ... I'm lost with Swift syntax when it comes to create expressions. Look at this basic example where I just try to find out if one string is contained into another by calling String.rangeOfString that returns an Optional Range (Range?)
This works as expected:
let LEXEMA:String="http://"
let longUrl:String="http://badgirls.many/picture.png"
let range=longUrl.rangeOfString(LEXEMA);
if (range? != nil) {
// blah
}
Now I'm trying to combine the expression inside the if, something like:
if (longUrl.rangeOfString(LEXEMA)? !=nil) {
// blah
}
But I always get syntax errors, the above yields a "Expected Separator" and can't understand why. Done some more tests:
if (absolutePath.rangeOfString(URL_LEXEMA) !=nil) { }
Expected Separator before "!"
if absolutePath.rangeOfString(URL_LEXEMA) !=nil { }
Braced block of statements is an unused closure
What am I doing wrong?
If you’re coming from other like Java, you might be thinking of optionals like pointers/references, and so used to equating them to nil and if non-nil, using them. But this is probably going to lead to more confusion. Instead, think of them like a container for a possible result, that you need to unwrap to use. if let combines the test and unwrapping operation.
With this in mind, here’s how you could adapt your code:
let LEXEMA: String="http://"
let longUrl: String="http://badgirls.many/picture.png"
if let range = longUrl.rangeOfString(LEXEMA) {
// use range, which will be the unwrapped non-optional range
}
else {
// no such range, perhaps log an error if this shouldn’t happen
}
Note, that ? suffixing behaviour you were using changes in Swift 1.2 so even the code in your question that compiles in 1.1 won’t in 1.2.
It’s possible that sometimes you are whether there was a value returned, but you don’t actually need that value, just to know it wasn’t nil. In that case, you can compare the value to nil without the let:
if longUrl.rangeOfString(LEXEMA) != nil {
// there was a value, but you don't care what that value was
}
That said, the above is probably better expressed as:
if longUrl.hasPrefix(LEXEMA) { }
For starters:
You don't need parenthesis with if statements unless you have nested parenthetical subexpressions that require it.
You don't need to specify the type on the left side of the = of a let or var declaration if Swift can figure it out from the right side of the =. Very often Swift can figure it out, and you can tell that Swift can figure it out, so you can avoid that redundant clutter.
You do need to specify the type if Swift cannot figure out the type from
the right side. Example:
For example, consider the following lines:
let LEXEMA = "http://"
let longUrl = "http://badgirls.many/picture.png"
Swift can figure out that they're strings.
Similarly for this function or class that returns a UIView:
var myView = ViewReturningClassOrFunc()
Consider this:
#IBOutlet var myView : UIView!
In the above line, Swift cannot figure out ahead of time it will be assigned a UIView, so you have to provide the type. By providing a ! at the end you've made it an implicitly unwrapped optional. That means, like ?, you're indicating that it can be nil, but that you are confident it will never be nil at the time you access it, so Swift won't require you to put a ! after it when you reference it. That trick is a time saver and big convenience.
You should NOT add the ? to the line:
if (longUrl.rangeOfString(URL_LEXEMA) !=nil) {
As another answer pointed out, you're missing the let.
if let longUrl.rangeOfString(URL_LEXEMA) {
println("What do I win? :-)")
}
swift is case sensitive language. you need to check about whitespaces as well
if longUrl.rangeOfString(LEXEMA) != nil {
//your condition
}
there should be space between statement != nil
Just add a space between != and nil like:
if longUrl.rangeOfString(LEXEMA) != nil {
// blah
}
I tested your code in playground, an error of Expected ',' separator reported.
And do not forget the rules that 1s and 0s and Airspeed Velocity said.
I came across a conditional if statement in Objective-C:
self.frontCardView = self.backCardView;
if ((self.backCardView = [self popPersonViewWithFrame:[self backCardViewFrame]])) {
// Fade the back card into view.
...
}
Basically:
if ((self.backCardView = self.popPersonViewWithFrame(self.backCardViewFrame()))) {...}
This sets "self.backCardView" to the return value of
"-popPersonViewWithFrame:". In C (and Objective-C), the result of an
assignment is the assigned value.
In this case, the result of the expression "(self.backCardView = [self
popPersonViewWithFrame:self.backCardViewFrame])" the return value of
"-popPersonViewWithFrame:".
If the return value is "nil", then the conditional is not executed (since "nil" is a false value).
If I try to do the same thing in Swift:
self.frontCardView = self.backCardView
if ((self.backCardView = self.popPersonViewWithFrame(self.backCardViewFrame()))) {
// Fade the back card into view.
...
}
I get an error in compilation:
Type '()' does not conform to protocol 'LogicValue'
Swift was specifically designed to not allow testing an assignment in a conditional for safety reasons (people accidentally using one = instead of two). The result of an assignment operator is always void () like the error says.
Assign, then check for nil separately.
self.frontCardView = self.backCardView
self.backCardView = self.popPersonViewWithFrame(self.backCardViewFrame())
if self.backCardView != nil {
// Fade the back card into view.
// ...
}
() is simply a typealias for void, which is what assignments return in Swift. As Bryan suggested, just put the assignment outside of the condition.
The condition isn't a condition, so like Bryan Chen said, do the assignment outside of the condition, but assign it to another variable. In the condition, then, check whether that variable is equal to backCardView, like so:
frontCardView = backCardView
let poppedView = self.popPersonViewWithFrame(self.backCardViewFrame())
if backCardView == poppedView {
// Fade the back card into view.
...
Ok I think my understanding of properties in objective c may not be what I thought it was.
In my program I have a singleton that contains my class.
In my class during the init I assign a value from the singleton to my property.
I then assign a value to a property of that property.
However it does not keep the value and when I do a compare of the value in the singleton nothing has changed. What is going on here? Any ideas?
#interface MainGameLoop : NSObject {
MapData *mapData;
}
#property (retain) MapData *mapData;
-(id) init
{
self = [super init];
GlobalVariables *sharedManager = [GlobalVariables sharedManager];
self.mapData = sharedManager.mapData;
return self;
}
In a function of my class:
works:
sharedManager.mapData.currentPlayer = newCurrentPlayer;
does nothing:
self.mapData.currentPlayer == newCurrentPlayer;
self.mapData.currentPlayer == newCurrentPlayer;
Are you sure that you want two equal signs there? That statement is syntactically correct and will evaluate to either true or false.
== is a Boolean operator, while = is an assignment operator. Like what Dave said, if you are using an if statement such as if (self.mapData.currentPlayer == newCurrentPlayer) {…}, you would want to use == because it would evaluate to true or false, while = would be used to set the value of a variable, which is what I think you are trying to do.
If it's any consolation, I've made that mistake too many times to count…
Something that I do is to use NSLog() or printf() to make sure that each step is working correctly.