macOS 10.15.7: String(init(unsafeUninitializedCapacity:initializingUTF8With:)

Xcode let's me use this initializer in my iOS 13.4 targeted project:

String(init(unsafeUninitializedCapacity:initializingUTF8With:)

But when I open my macOS 10.15 targeted package, the function is simply is not recognized by Xcode. As if it did not exist. I have the same problem from the command line:

> swift --version        
Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Target: x86_64-apple-darwin19.6.0
> swift package --version
Swift Package Manager - Swift 5.3.0
> swift build
...
6:21: error: extraneous argument label 'unsafeUninitializedCapacity:' in call
  let cafe1 = String(unsafeUninitializedCapacity: validUTF8.count) {

Here is part of Package.swift:

// swift-tools-version:5.3
import PackageDescription
let package = Package(
  name: "abc",
  platforms: [.macOS(.v10_15)],
  products: [

I am running Xcode 12.0.1 (12A7300), 12.Catalina 10.15.7
Do I need a newer version of the Swift package manager? Any other ideas?

Initializers can be called in two ways in Swift: in this case you can initialize a String either with

let str = String(unsafeUninitializedCapacity: ..., initializingUTF8With: ...)

or by using .init on the type explicitly

let str = String.init(unsafeUninitializedCapacity: ..., initializingUTF8With: ...)

You can find some examples of that particular initializer in the appropriate documentation page.

I get that - but it has nothing to do with my question. What I used was the example in the documentation. Works for my IOS project, not for my macOS project.

My bad. If you cmd + click on the initializer and then click on Jump to Definition you will see:

@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public init(unsafeUninitializedCapacity capacity: Int, initializingUTF8With initializer: (UnsafeMutableBufferPointer<UInt8>) throws -> Int) rethrows

so, that initializer is only available on macOS 11.0 (Big Sur). It's also marked as available on iOS 14, are you sure you run your app on iOS 13.4?

1 Like

Thanks, great answer. Maybe I'll download macOS 11.0 Beta. Any idea when macOS 11 will be released?

Before the end of 2020, at worst.

You're right it's iOS14.

Big Sur 11 should be released in this month.

I'm new to swift and installed xcode, followed the hello tutorial, and got the same message you did. So, after reading this, I updated to the beta, and I am still getting that error.

What version of MacOS/iOS are developing on? What version of MacOS/iOS are you deploying to? Deployment target has to be MacOS 11.0 (has not been released yet) for macOS, iOS14 for iOS, similar for iPAD OS.