Cannot convert ZoneConfiguration to ZoneOptions - ios

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.

Related

How to fix the Strideable protocol error?

I have the following code, but the compiler gives an error
Type 'String.Index' does not conform to protocol 'Strideable'
While I was looking for, I encountered many similar questions, tried several suggested options - but this did not help get rid of the problem.
I'm sorry, I'm not very good at this and just trying to run the code of old version of Swift in my Swift 4 project.
Error on the line
roz.endIndex
let t = myHTMLString
let rangeOfZero = t.range(of: "[\"fmt_stream_map\"", options: .backwards)
if let roz = rangeOfZero {
if let suffix = t.characters.suffix(roz.endIndex) {
first = suffix
}
}
I suppose you want to get the substring from the end of the found range to the end of the string.
In Swift 4 you can use
let t = myHTMLString
if let roz = t.range(of: "[\"fmt_stream_map\"", options: .backwards) {
first = String(t[roz.upperBound...])
}

Cannot call value of non-function type '((AnyObject) -> AnyObject?)!' - Swift 3

I am trying to convert my current project to Swift 3 and am running into problems regarding the objectForKey to objectFor conversion.
The type of myData is AnyObject
Before:
let x = myData.objectForKey('myKey')
Now:
let x = myData.objectFor('myKey') // not working.
In the second case, XCode gives me error Cannot call value of non-function type '((AnyObject) -> AnyObject?)!' which I have no idea how to fix.
Does anyone know how to updated the objectForKey method and why this conversion is not working?
Other Example:
I created another example in a playground to reproduce this issue. The lines of code used are pasted below. The outcome of running this is the same error as stated above.
var x: AnyObject = ["Hello" : "Goodbye"] as AnyObject
x.objectFor("Hello")
The syntax has changed from:
let x = myData.objectForKey("myKey")
to:
let x = myData.object(forKey:"myKey")
in Swift 3.
Example:
var x : AnyObject = ["Hello" : "Goodbye"] as AnyObject
x.object(forKey:"Hello")

`CountedSet` initialization issue

I'm comparing the characters contained within two words. In seeking to accomplish this, Set (aka NSSet) seemed like the way to go to accomplish this task. I've discovered it returns false positives on matches, so I am attempting to use CountedSet (aka NSCountedSet) instead.
I'm able to initialize a Set without issue, but I can't get the CountedSet initializer to work. Here's what I've done...
I start with a String:
// Let's say myTextField.text = "test"
let textFieldCharacters = myTextField.text?.characters
// word is a string from the ENABLE list of words
let wordCharacters = word.characters
Then I dump the characters into an Array:
var wordCharactersArray = [Character]()
for character in wordCharacters {
wordCharacterArray.append(character)
}
var textFieldCharactersArray = [Character]()
for character in textFieldCharacters {
wordCharacterArray.append(character)
}
Then I create a Set from the character arrays:
let textFieldSet = Set<Character>(textFieldCharactersArray)
let wordSet = Set<Character>(wordCharactersArray)
Finally, I test to see if the textFieldSet is a superSet of wordSet with the following:
textFieldSet.isSuperset(of: wordSet)
Going back to my example, if myTextField.text is "test", I'm returning values for word whose characters are a superset of the wordSet, but the counts of the individual elements don't match the character counts of myTextField.text
In researching my issue, I've found CountedSet (fka NSCountedSet), which I think would resolve my issue. It has two method signatures:
public convenience init(array: [AnyObject])
public convenience init(set: Set<NSObject>)
I've tried initializing the 2 sets of characters like so:
let textFieldSet = CountedSet(array: textFieldCharacterArray)
let wordSet = CountedSet(array: wordCharacterArray)
I get the following error for the sets
Cannot convert value of type '[Character]' to expected argument type
'[AnyObject]'.
So I tried initializing the set like this:
let textFieldSet = CountedSet(array: textFieldCharacterArray as! [AnyObject])
Which yields the following error:
'AnyObject' is not a subtype of 'Character'
I've also tried to initialize the CountedSet with a Set, per the method signature, but I get errors when I try to do that, too.
Any suggestions how to initialize a CountedSet would be greatly appreciated.
You are correct that if you need to compare not just the presents of elements but also their count, you should use CountedSet, which is a renaming of NSCountedSet for swift 3.0. The problem you are running into is CountedSet can only accept elements that are objects and Characters are not. As Eric D points out in their comment, the easies way to get around this is by mapping your [Character] to [String] which will bridge to [NSString].
You are not running into this problem using Set, because it is a native Swift collection type that initialize with elements of any type. This is why you can initialize a Set with [Character].
To see the difference:
let word = "helo"
let wordCharacters = Array(word.characters)
let wordSet = Set(wordCharacters)
let wordCharStrings = wordCharacters.map{String($0)}
let wordCountedSet = CountedSet(array: wordCharStrings)
let textField = "hello"
let textFieldCharacters = Array(textField.characters)
let textSet = Set(textFieldCharacters)
let textFieldCharStrings = textFieldCharacters.map{String($0)}
let textFieldCountedSet = CountedSet(array: textFieldCharStrings)
textFieldCountedSet.isSubset(of: wordCountedSet as! Set<NSObject>) // returns false, but if word had two or more l's it would return true
textSet.isSubset(of: wordSet) // returns true

CMVideoFormatDescriptionGetCleanAperture() swift error

I can't get the CMVideoFormatDescriptionGetCleanAperture() function to work. Using
var videoDescriptionRef = port.formatDescription as CMVideoFormatDescriptionRef
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(videoDescriptionRef, true)
or
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(port.formatDescription, true)
gives me the following errors, respectively:
Cannot convert expression's type 'CMVideoFormatDescriptionRef' to type 'CMVideoFormatDescriptionRef'
And the second is
Could not find an overlad for '__conversion' that accepts the supplied arguments
Does anyone know how to fix these or can anyone point out any errors in getting the format description for retrieving the clean aperture?
CMFormatDescription itself does not have a member takeUnretainedValue. It is a member of Unmanaged<T>. You need to define an unmanaged variable first, get a value into it and then you retain it.
As I do not know how you created port.formatDescription, here is an example where formatDescription is created from PPS/SPS from an AVC video.
// create the receiving unmanaged variable
var formatDescription: Unmanaged<CMVideoFormatDescription>?
// call the not annotated method, this method will fill in formatDescription which is Unmanaged<T>
let result = CMVideoFormatDescriptionCreateFromH264ParameterSets(nil, UInt(parameterSets.count), parameterSets, parameterSetSizes, 4, &formatDescription)
// check result, yada yada yada before continuing
// now we can actually retain the value
let formatDescriptionRef = formatDescription!.takeUnretainedValue() as CMVideoFormatDescriptionRef
// and we can do what ever we want with it in Swift without caring anymore.
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(formatDescriptionRef, 1)
Create an unmanaged type
Use this unmanaged type in an not annotated method
Retain the value
Work with it without thinking about how you created it.
So adjust your code where you create the port.formatDescription as a Unmanaged and you should be good.
Try to use .takeUnretainedValue() like this:
var videoDescription : CMVideoFormatDescription = port.formatDescription.takeUnretainedValue()
var cleanAperture = CMVideoFormatDescriptionGetCleanAperture(videoDescription, true)

Another twist on Type Providers

Trying to extend the boundary of the usage of type provider, I figured I could use :
//Does not work
let exceldatatype = MyExcelTypeProvider.ExcelFile<filename="Brokernet.xls">
let file = new exceldatatype()
let f1(elem:exceldatatype .Row) =
elem.BID
this works though :
//Works
let file= MyExcelTypeProvider.ExcelFile<filename="Brokernet.xls">()
let f1(elem:MyExcelTypeProvider.ExcelFile<filename="Brokernet.xls">.Row) =
elem.BID
This does not work either :
//Does not work
let typealias = MyExcelTypeProvider.ExcelFile<filename="Brokernet.xls">.Row
I guess there is some deep lesson to be found there about runtime and compile time properties, but I am not sure what.
UPDATE
As specified by the ever helpful kvb, the following works :
type typealias = MyExcelTypeProvider.ExcelFile<filename="Brokernet.xls">
let file2 = new typealias()
In your first line, try type ... = ... instead of let ... = ....

Resources