The below code works well with "Swift 3" but giving error of "unresolved identifier CALayerContentsGravity" in "Swift 4"
static func create(image: UIImage, size: Double) -> CALayer {
let containerLayer = createContainerLayer(size)
let imageLayer = createContainerLayer(size)
containerLayer.addSublayer(imageLayer)
imageLayer.contents = image.cgImage
imageLayer.contentsGravity = CALayerContentsGravity.resizeAspect
return containerLayer
}
In swift4 CALayerContentsGravity is not working.
To set contentsGravity of layer you should use constant string which is available in swift4.
imageLayer.contentsGravity = kCAGravityResizeAspect
Hope it will work.
i had this problem after I imported the Cosmos pod. I spoke to the author and he said I need to check the pod's target and the Swift language it used.
When I check the Cosmos target the Swift version was 4 but my main project used Swift 4.2. Once I changed the Cosmos pod's version from 4 to 4.2 the error went away.
suggestion from Cosmos author
This is how you check the Cosmos pod Swift version
It needs to match what's in your main project's target:
Related
I am upgrading my Swift project from 2.3 to 3.0 in XCode 8. I use Google Maps API and they have some boilerplate code as follows:
self.mapView.camera=camera
let marker = GMSMarker(position: camera.target)
marker?.snippet = "Hello World"
marker.map = self.mapView
However, my XCode 8 Swift compiler gives me an error on the last line:
Cannot assign to property; map is a method
I have no control over the properties in Google's GMSMarker class. So how can I set the 'map' property?
Curious, what would this be in Swift 4?
stimageout = AVCapturePhotoOutput()
stimageout?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
Currently, it errors with Value of type 'AVCapturePhotoOutput' has no member 'outputSettings' which is odd since I don't have memory of Apple changing this.
This is not "begging for help"-type question. I'm just curious if Apple changed this and the steps I need to do in order to fix this issue.
Thanks in advance. :)
The problem is outputSettings is a property on AVCaptureStillImageOutput, not AVCapturePhotoOutput.
AVCaptureStillImageOutput is deprecated in iOS 10, so for iOS 10+, use AVCapturePhotoOutput instead. To set settings using the new API, you can use an AVCapturePhotoSettings object.
let stimageout = AVCapturePhotoOutput()
let settings = AVCapturePhotoSettings()
settings.livePhotoVideoCodecType = .jpeg
stimageout.capturePhoto(with: settings, delegate: self)
Apple's AVCapturePhotoOutput Documentation: https://developer.apple.com/documentation/avfoundation/avcapturephotooutput
AS I have updated the XCode to 8, new version, the following code
func center(_ usePresentationLayer: Bool) -> CGPoint
{
if usePresentationLayer, let presentationLayer = layer.presentation() as! CALayer
{
return presentationLayer.position
}
return center
}
result in
Error - Value of type 'CALayer' has no member 'presentation'
How can I fix this issue in XCode using swift 3.0?
Edit: the question changed
Make sure request is of type URLRequest.
Don't upload screenshots of code - please edit your post so it contains code that's searchable.
I'm following a basic tutorial on building a simple iOS app in Swift.
It is written in Swift 2.x, and I work with XCode 8 Beta and Swift 3.
The tutorial suggests using NSFileManager to find a data directory. Class names have changed, so the auto-fixed Swift 3 looks like this:
static let DocumentsDirectory = FileManager().urlsForDirectory(.documentDirectory, inDomains:.userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals")
However, Xcode now complains that
Value of type 'URL' has no member 'URLByAddingPathComponent'
I am unable to find out what the method is called now.
The NSURL Class Reference doesn't contain any hints as to how to address it from Swift 3
What is the new method name?
Where do I have to go to find a complete class reference for Swift 3 (or, the Swift 3 interface to the library the URL class is defined in - I still don't completely understand the nomenclature) so I can research these myself in the future?
As of Xcode 8 beta 4, it is named appendingPathComponent(_:), and does not throw.
static let archiveURL = documentsDirectory.appendingPathComponent("meals")
Also as Leo Dabus points out in the comments, your documentsDirectory property will need changing to use urls(for:in:) in beta 4:
static let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
(Note that I've made your property names lowerCamelCase, as per the Swift API design guidelines. I would also recommend using FileManager.default, rather than creating a new instance.)
You can take a look at Apple's latest API reference guide to see the API naming changes that have taken place in Swift 3.
It has now changed to appendingPathComponent(_:) and it throws, so you need to wrap it in do - catch block
do {
let archiveURL = try documentsDirectory?.appendingPathComponent("meals")
} catch {
print(error)
}
Update
As per Xcode 8 beta 4, the appendingpathcomponent(_:) do not throw error.
For relevant info see answer by #Hamish
func appendingPathComponent(String)
=> Returns a new URL made by appending a path component to the original URL.
static let archiveURL = documentsDirectory?.appendingPathComponent("meals")
If it is directory:
func appendingPathComponent(String, isDirectory: Bool)
=> Returns a new URL made by appending a path component to the original URL, along with a trailing slash if the component is designated a directory.
static let archiveURL = documentsDirectory?.appendingPathComponent("meals", isDirectory: true)
First very appreciate for your help. I just upgraded Xcode yesterday which contains swift 2.2. I've faced a few issues but I fixed them quickly by following the "what's new in swift 2.2" topics from Natashatherobot. But there is one issue I cannot fix. It's about failable initializers of UIFont which was introduced in swift 2.2. Attached is a simple piece of code that will report error in swift 2.2. It might not report the error immediately, until I cleaned the project.
lazy var somelabel: UILabel = {
let label = UILabel()
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10) //this line gave me error
label.font = font
label.text = "Calculating..."
return label
}()
Here is the screenshot of the error
The error is : (name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'
I can fix it in two ways:
Method 1: don't put this line:
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10)
in the 'lazy instantiation' closure. (Put it in computed properties reports no error)
Method 2: instead of using:
UIFont(name: "somefont", size: 10)
use the below instead( However I don't think this should be the right approach because it makes the initializer more "objc" style):
UIFont.init(name: "somefont", size: 10)
But I still don't understand why it would report me error in the lazy property closure. I will be very appreciated if someone can give me some explanations.
This might be a bug of the latest version xcode. Those whose project was set up before the upgrade might face this problem. Anyone who is luck enough to face such issue can try to start a new project in swift 2.2 environment.