swift

Xcode 12.5: SPM Dependency Cache Location

孤人 提交于 2021-02-19 05:58:05
问题 Swift Package Manager got a new feature in Xcode 12.5: Swift Package Manager caches package dependencies on a per-user basis, which reduces the amount of network traffic and increases performance of dependency resolution for subsequent uses of the same package. If needed, you can disable cache use in xcodebuild by using the new -disablePackageRepositoryCache flag. (72204929) I would like to know where this is cached. Maybe we can use this to easily cache those dependencies in continuous

Protocol variable implementing another protocol

只谈情不闲聊 提交于 2021-02-19 05:53:26
问题 I'm trying to do something like that, but then, ParentC doesn't conform to Parent because its children member isn't Child but ChildC Which is weird because ChildC implements Child ... Is this a limit of Swift? Or is there is a way of doing that? (I do not ask for an alternative solution, I want to know if this is possible) protocol Parent: Codable { var children: [Child] { get } } protocol Child: Codable { var name: String { get } } struct ParentC: Parent { var children: [ChildC] } struct

Audiokit trim audio

风流意气都作罢 提交于 2021-02-19 05:46:05
问题 I'm making audio editing app, and I want trim my audio. For audio editing I use AudioKit framework. But I can't find in tutorials, and examples, how I can trim audio using this framework? 回答1: Export asynchronously allows for setting the start and end samples: https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/Internals/Audio%20File/AKAudioFile%2BProcessingAsynchronously.swift#L267 /// Exports Asynchronously to a new AKAudiofile with trimming options. /// ... /// - fromSample:

Creating a random image generator with Swift

佐手、 提交于 2021-02-19 05:25:31
问题 I'm trying to make a random image appear on the screen, but I'm new to Swift and am not sure how to do this. I have three images which I want have randomly shown in the image view, when the app is opened. How do I do this? 回答1: Generate a ramdom number from 0 to 2 and show the image by randomly generated number. var random = arc4random_uniform(3) //returns 0 to 2 randomly switch random { case 0: //show first image case 1: //show second image default: //show third image } 回答2: If the images

How to get Height of Safe Area Programmatically Prior to IOS 11?

断了今生、忘了曾经 提交于 2021-02-19 05:19:08
问题 Without using safeAreaLayoutGuide (I am targeting IOS 9+), is there any way to programmatically get the height of the "safe area" in IOS without having to create a new view (constrained to the safe area) solely for this purpose? I can't set an outlet to the safe area because it's not a UIView... or even a class of any sort. And if I simply use self.view.height in the ViewController, it's going to be too high (wrong). Is there some other way to do it? 回答1: In a UIViewController you can use the

How to get Height of Safe Area Programmatically Prior to IOS 11?

谁说胖子不能爱 提交于 2021-02-19 05:19:07
问题 Without using safeAreaLayoutGuide (I am targeting IOS 9+), is there any way to programmatically get the height of the "safe area" in IOS without having to create a new view (constrained to the safe area) solely for this purpose? I can't set an outlet to the safe area because it's not a UIView... or even a class of any sort. And if I simply use self.view.height in the ViewController, it's going to be too high (wrong). Is there some other way to do it? 回答1: In a UIViewController you can use the

Color from Green to Red with Percentage

杀马特。学长 韩版系。学妹 提交于 2021-02-19 04:58:45
问题 i am trying to create a custom UIColor from Green to Red depends on a Percentage Value, but i have no idea how can i do something like that? Any ideas? 回答1: Something along these lines should work, if you just want a linear mix: func mixGreenAndRed(greenAmount: Float) -> UIColor { return UIColor(red: (1.0 - greenAmount), green: greenAmount, blue: 0.0, alpha: 1.0) } That one will blend through RGB (0.5, 0.5, 0), though—a kind of ugly orange—so you might want to do this instead, which will just

Saving a Codable Struct to UserDefaults with Swift

冷暖自知 提交于 2021-02-19 04:43:08
问题 I am trying to encode a struct struct Configuration : Encodable, Decodable { private enum CodingKeys : String, CodingKey { case title = "title" case contents = "contents" } var title : String? var contents: [[Int]]? } into JSON to store in a local key of UserDefaults.standard. I have the following code: let jsonString = Configuration(title: nameField.text, contents: newContents) let info = ["row" as String: jsonString as Configuration] print("jsonString = \(jsonString)") //trying to save

How to implement a Swift protocol across structs with conflicting property names

谁说我不能喝 提交于 2021-02-19 04:24:43
问题 I'm trying to write a protocol that reconciles two different structs that describe the same concept, a stop of some kind. Both have a Code , Description , and Latitude & Longitude coordinates, but for one type, the Description could be nil , and for the other type, the coordinates might be nil . How can I write a single protocol that reconciles these two structs? Here's my protocol: protocol Stop { var Code : String { get } var Description : String { get } var Latitude : Double { get } var

convert an std::string to a Swift String

天大地大妈咪最大 提交于 2021-02-19 04:24:26
问题 Short version: how can I convert an std::string (object returned by a .cpp function called from a .Swift file using bridging) to a Swift String ? Long version: I have a library written in C++ and I have to call some code using Swift. I created a bridge, adding two files in my Xcode projects: a bridging header, which allows Swift to call C functions (afaik Swift can't call C++ functions directly, so it needs to pass through C functions) //file bridgingHeader.h const char * getLastOpenedFile();