Don't understand "Extra argument 'repeatedValue' in call" - ios

What could be wrong with:
let myArray = [UInt](count: 256, repeatedValue: 0)
that leads to the error Extra argument 'repeatedValue' in call?
I found this in existing code I'm adding to my Swift 5 app.

This is not Swift 5. The initializer name is init(repeating:count:) since Swift 3.

Related

Swift 3: Expression implicitly coerced from 'UIView?' to Any

Someone else must have received this message while (or after) converting an iOS project to Swift 3, yet when I do a Google search, I get no relevant results.
Anyway, after converting to Swift 3, I have about 30 warnings that say:
Expression implicitly coerced from 'UIView?' to Any
But the warnings do not point to any specific line of code. They only reference the class where the warning exists.
Does anyone have an insight into this warning or how I might go about silencing them?
In my case it was an issue related to a dictionary without explicit type:
let dict = ["key": value]
Than I solved specifying the type:
let dict: [String: Any] = ["key": value]
In your case you can specify your value type:
let dict: [String: UIView] = ["key": value]
This will happen when the function you are calling has a parameter of type Any, and you are passing an optional.
For example:
let color: UIColor? = UIColor.red
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color], for: .normal)
Notice that color is of type UIColor? and that setTitleTextAttributes expects a dictionary of type [String: Any]?.
In order to avoid the warning you have to either force unwrap your optional, or cast it to Any.
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color!], for: .normal)
or
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color as Any], for: .normal)
Looks like a bug in the Swift compiler:
https://bugs.swift.org/browse/SR-2921
Currently, I'm seeing this with Xcode 8.1 and 8.2 beta 1.
In your case, the warning should identify the source file, but not the line - as you stated. You will have to hunt around for calls to functions with Any parameters.
Good new is that it appears fixed in an upcoming Swift toolchain.
I believe this is fixed in Xcode 8.3 beta 1 (but have not confirmed)
Problem
The expected type is Any but the type provided was UIView?
The problem is with the optional, just make sure an instance of UIView is passed and things would work.

How to use UnsafeMutablePointer in Swift 3?

I have the following code written in Swift 2.2:
let keyData = NSMutableData(length: 64)!
SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
XCode 8 highlights that second line and claims that
Cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an
argument list of type '(UnsafeMutableRawPointer)'
While I appreciate XCode telling me this, I don't quite understand how to change the UnsafeMutableRawPointer to be acceptable.
Does anyone know how to convert this code into Swift 3?
I recommend you to work with Data rather than NSData in Swift 3.
var keyData = Data(count: 64)
let result = keyData.withUnsafeMutableBytes {mutableBytes in
SecRandomCopyBytes(kSecRandomDefault, keyData.count, mutableBytes)
}
withUnsafeMutableBytes(_:) is declared as a generic method, so, in simple cases such as this, you can use it without specifying element type.

Initializers in native arrays in swift

I am just getting started with swift and I know we have a default array initialiser in swift and the syntax goes like this:
let myArray = [Int](count:3 , repeatedValue:2)//int types [2,2,2]
But when I remove [Int] from the statement,it initialises the array with values (3,2).
let myArray = (count:3 , repeatedValue:2)//[3,2]
Can anyone explain this behaviour?
In the second example, you're getting a tuple, not an Array. If you don't want to specify [Int], you still need to specify Array, like this:
let myArray = Array(count: 3, repeatedValue: 2)
Learn more about tuples in the Swift book.

Getting number from String Swift 2 Issue

I was able to get the phone number from the textfield with the method below in Swift 1.2 but I'm trying the same method in Swift 2 but I'm getting an error from .join "join is unavailable". Please how can I write the same method in Swift 2?
let userNumber = NSNumberFormatter().numberFromString("".join(phoneNumberTextField.text!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)))
This answer didn't help me that I can't get join at all.
Cannot invoke `join` with an argument list of type (String, [String]) in Swift 2.0
Try this:
let numbers = phoneNumberTextField.text!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
let userNumber = numbers.joinWithSeparator(" ") // Using space as separator

Cannot invoke 'sequence' with an argument list of type '([AnyObject])'

I have upgraded to Xcode 7-beta and it gives me this error: Cannot invoke 'sequence' with an argument list of type '([AnyObject])'. That error is in this line of code:
sprite.runAction(SKAction.sequence(actionarray as [AnyObject]))
I found that in swift 2 I must remove part of it and it must look like this:
sprite.runAction(SKAction.sequence(actionarray))
But actionarray in NSMutableArray and now it gives me this error: Cannot invoke 'sequence' with an argument list of type '(NSMutableArray)'
This is the content of NSMutableArray:
var actionarray:NSMutableArray = NSMutableArray()
actionarray.addObject(SKAction.moveTo(CGPointMake(self.frame.size.width/2, -sprite.size.height), duration: NSTimeInterval(duration)))
actionarray.addObject(SKAction.removeFromParent())
sprite.runAction(SKAction.sequence(actionarray))
It worked well in Xcode 6. What should I change there?
Thanks
Why do you use NSMutableArray in Swift code in the first place?
Try replacing with Swift array like this (compiles in Playground):
import Cocoa
import SpriteKit
let sprite = SKSpriteNode()
var actionarray: [SKAction] = []
actionarray.append(SKAction.moveTo(CGPointZero, duration: NSTimeInterval(1.0)))
actionarray.append(SKAction.removeFromParent())
sprite.runAction(SKAction.sequence(actionarray))
Try using this syntax:
SKAction.sequence(actionarray as AnyObject as [SKAction])

Resources