Swift 3 conversion gets 'Int1' is not convertible to 'Bool' - ios

I'm converting Swift 2 code that compiles and runs to Swift 3 and am getting the following error:
'Int1' is not convertible to 'Bool'
The code is as follows:
isUpdated = sharedInstance.database!.executeUpdate(
"UPDATE Coin_Table SET upgrade=?, grade=?, WHERE coin_id=?",
withArgumentsInArray: [
coinInfo.upgrade, (coinInfo.grade != nil) ? coinInfo.grade! : NSNull(),
coinID])
The code above is using FMDB with the method defined in FMDB.h as
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
When compiling my code above it flags the "(coinInfo.grade !=" and gives the error.
I tried simplifying it to see if it would still happen:
let theArray: NSArray = [true ? "foo" : NSNull()]
and still get the same error, this time it flags the "true".
I've done a bunch of searches on this and haven't found anything close other than https://bugs.swift.org/browse/SR-2372 but that is an issue with tuples which I wouldn't think would affect my code.
Can anyone shed some light on this or suggest a workaround if it is a compiler bug?
Thanks

As you wrote yourself your issue is the same as a one described here. Bugs happens ))
Why not just to use a temporary variable to fix it:
let hasGrade: Any = (coinInfo.grade != nil) ? coinInfo.grade! : NSNull()
isUpdated = sharedInstance.database!.executeUpdate(
"UPDATE Coin_Table SET upgrade=?, grade=?, WHERE coin_id=?",
withArgumentsInArray: [
coinInfo.upgrade, hasGrade,
coinID])

Related

Interview question Swift, incomprehensible syntax

In the WWW I find interview question and one of them is:
What will be the output of the code snippet below:
class Kondana<T:Equatable> {
var dictDataHolder = [String:T]()
func add(value:T?,using key:String) -> T? {
self.dictDataHolder[key] = value
return value
}
}
var fortOne = Kondana<String>()
let value = fortOne.
print(value)
Answers are:
a) British
b) nil
c) compile-time error
d) segmentation fault
I am run this code in Xcode and my output is:
__lldb_expr_21.Kondana<Swift.String>
Help me please understand what is going on step by step.
I have thoughts about what is going on but not sure about them.
What is Kondana class and why use the syntax like above, I know that is generic but not understand the output?
With the code posted, the correct answer is "compile-time error". As others have said, the line
let value = fortOne.
is truncated, and is therefore not legal.

NSPredicateEditorRowTemplate() gives syntax error "Type of expression is ambiguous without more context"

I fail in creating a NSPredicateEditorRowTemplate
func predicator() -> NSPredicateEditorRowTemplate {
let monthList = [NSExpression(forConstantValue:1),NSExpression(forConstantValue:2), NSExpression(forConstantValue:3) ]
let leftExp = NSExpression(forKeyPath:"monthP")
let monthTemplate = NSPredicateEditorRowTemplate (
leftExpressions: leftExp,
rightExpressions: monthList,
modifier: NSComparisonPredicate.Modifier(rawValue: 0),
operators: NSNumber(value:4),
options: 0)
return monthTemplate
}
When the code gets compiled, I receive the error message:
"Type of expression is ambiguous without more context"
I checked , if I have missed an argument, and if arguments are typed correctly. All seems ok. I even used the code completion help to avoid mistyping arguments.
No idea what could be wrong. Does anyone see the problem ?

Cannot convert ZoneConfiguration to ZoneOptions

As ZoneOptions is deprecated, I changed optionsByRecordZoneID variable to ZoneConfiguration as follows
var optionsByRecordZoneID = [CKRecordZone.ID: CKFetchRecordZoneChangesOperation.ZoneConfiguration]()
for zoneID in zoneIDs {
let options = CKFetchRecordZoneChangesOperation.ZoneConfiguration()
options.previousServerChangeToken = settings.getChangeToken(forKey: databaseTokenKey)
optionsByRecordZoneID[zoneID] = options
}
Now, I am getting the following error for this line for optionsByRecordZoneID variable,
let fetchRecordZoneChangesOperation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zoneIDs, optionsByRecordZoneID: optionsByRecordZoneID)
Cannot convert value of type '[CKRecordZone.ID :
CKFetchRecordZoneChangesOperation.ZoneConfiguration]' to expected
argument type '[CKRecordZone.ID :
CKFetchRecordZoneChangesOperation.ZoneOptions]?'
Any help in regard to get rid of it would be appreciated.
The init(recordZoneIDs:,optionsByRecordZoneID:) is deprecated too since it takes the old ZoneOptions.
Use init(recordZoneIDs:,configurationsByRecordZoneID:).
let fetchRecordZoneChangesOperation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zoneIDs, configurationsByRecordZoneID: optionsByRecordZoneID)
Just add this entry to state that the accepted answer it is the way to do it in Xcode 10.2. Please take a look at it.

participantQuitInTurnWithOutcome:nextParticipants:turnTimeout:matchData:completionHandler: in Swift - xcode compile error

When I attempt to use
participantQuitInTurnWithOutcome:nextParticipants:turnTimeout:matchData:completionHandler:
in Swift (found in GameKit), I get the following Xcode compile error...
"Extra argument 'turnTimeout' in call".
If I take out the turnTimeout: argument, Xcode complains with,
"Missing argument for parameter 'turnTimeout' in call".
Has anyone else encountered this and is there any way to solve? I obviously can't satisfy each requirement.
func turnBasedMatchmakerViewController(viewController: GKTurnBasedMatchmakerViewController!, playerQuitForMatch match: GKTurnBasedMatch!) {
var nextParts:Array<GKTurnBasedParticipant> = []
for participant in match.participants {
if (participant.matchOutcome == GKTurnBasedMatchOutcome.None) {
nextParts.append(participant as GKTurnBasedParticipant)
}
}
match.participantQuitInTurnWithOutcome(matchOutcome: GKTurnBasedMatchOutcome.Quit, nextParticipants: nextParts, turnTimeout: nil, matchData: self.currentMatch, completionHandler: nil)
}
MartinR got me thinking in the right direction. It turned out that I was passing the match as the matchData argument. They are obviously two different objects.
MartinR - please post your suggestion as an answer and I will accept.

Create PDF in Swift

I am following Apple's Docs to create a PDF file using Xcode6-Beta6 in Swift
var currentText:CFAttributedStringRef = CFAttributedStringCreate(nil, textView.text as NSString, nil)
if (currentText) { // <-- This is the line XCode is not happy
// More code here
}
Compiler throws Type 'CFAttributedStringRef' does not conform to protocol 'BooleanType' error
If I use if(currentText != nil) I get 'CFAttributedStringRef' is not convertible to 'UInt8'
From Apple's Docs for CFAttributedStringCreate
Return Value
An attributed string that contains the characters from str and the attributes specified by attributes. The result is NULL if there was a problem in creating the attributed string. Ownership follows the Create Rule.
Any idea how to resolve this? Thanks!
First you have to give it an explicit optional type (using the ?):
var currentText: CFAttributedStringRef? = ...
Then you can compare it to nil:
if currentText != nil {
// good to go
}
Your code compiles at the moment, because Apple hasn't yet "swiftified" CoreFoundation to return properly annotated types.
Be prepared that in the final release your code will not even compile, forcing you to use the optional type.

Resources