Swift 4.2 有一个特殊的条件canImport,可以帮助开发者检查一个模块是否可以导入到项目中。它是在 Swift 4.1 中引入的。
现在我正在研究用 Objective-C 编写的 iOS 项目。我使用模块,对于每个目标,这些模块都是不同的。这就是为什么我想使用这样的东西:
#if canImport(SomeModule)
@import SomeModule;
#endif
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?现在我为每个目标使用不同的“其他 C 标志”,但我想找到更灵活的解决方案。
今天我将我的Xcode从v.9.4.1更新到v.10.0.现在我尝试构建我的tvOS项目,我看到以下错误:意外的重复任务:CopyPlistFile /Users/username/Library/Developer/Xcode/DerivedData/MyApplication/Build/Products/Debug-appletvos/MyApplication.app/Settings.plist /Users/username/project_folder/Settings.plist(在目标'MyApplicationTarget'中).我试图重新安装pods(我使用CocoaPods 1.6.0 beta)并清理build文件夹,但它没有帮助.如何在Xcode 10中解决这个问题?
在我的 Swift 4.2.1 代码中,我有这个枚举:
enum MyEnum {
case caseOne(Int)
case caseTwo(String)
case caseThree
}
Run Code Online (Sandbox Code Playgroud)
它符合Equatable:
extension MyEnum: Equatable {
static func == (lhs: MyEnum, rhs: MyEnum) -> Bool {
switch (lhs, rhs) {
case (.caseOne, .caseOne), (.caseTwo, .caseTwo), (.caseThree, .caseThree):
return true
default:
return false
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要让它符合Hashable,这就是为什么我添加了一个扩展:
extension MyEnum: Hashable {
var hashValue: Int {
switch self {
case .caseOne:
return 1
case .caseTwo:
return 2
case .caseThree:
return 3
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想迁移到 …
在我用 Swift 4.2 编写的应用程序中,我有以下代码:
let arrayOfIntegers = [2, 1, 9, 5, 4, 6, 8, 7]
let unknownLowerBound = 4
let unknownUpperBound = 20
let closedRange = ClosedRange<Int>(uncheckedBounds: (lower: unknownLowerBound,
upper: unknownUpperBound))
let subRange = arrayOfIntegers[closedRange]
subRange.forEach { print($0) }
Run Code Online (Sandbox Code Playgroud)
正如您可以猜到的,当我运行此代码时,我收到以下错误:Fatal error: Array index is out of range。我想阻止它。
我的豆荚在这里
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'myAPP' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for myAPP
pod 'SwiftMessages'
pod 'IQKeyboardManager'
pod 'SwiftKeychainWrapper'
pod 'Tabman'
pod 'PagingMenuController'
pod 'Kingfisher'
pod 'Optik'
pod 'KRPullLoader'
pod 'AlamofireImage'
pod 'Firebase/Core'
pod 'Firebase/Database'
pod 'Firebase/Messaging'
pod 'Firebase/Auth'
pod 'Firebase/Storage'
pod 'TCPickerView'
pod 'GoogleMaps'
pod 'GooglePlaces'
pod 'Whisper'
pod 'Fabric' …Run Code Online (Sandbox Code Playgroud) 我不知道如何修复此错误“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”。我已经尝试了一切(我能做的)但不起作用
import SwiftUI
struct ContentView: View {
@State var content: String = "start"
var body: some View {
VStack { // Unable to infer complex closure return type; add explicit type to disambiguate
self.content = try! String(contentsOf: URL(string: "http://zyglarski.pl/swift-http-docs/tinder_list.php")!)
var contentData = self.content.data(using: .utf8)
var jsDec = JSONDecoder()
var responce = try! jsDec.decode(Candidates.self, from: contentData!)
Text(responce)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Candidates: Codable {
let candidates:[Candidate]
}
struct …Run Code Online (Sandbox Code Playgroud) 我想我正在做一些非常错误的事情,但我需要向您展示我的想法。我想画一个类似 180 度的半圆,它有三种不同的颜色。但我不想让它被填写,我只想让它成为圆圈本身。我正在使用这段代码:
func addCirc(colour: UIColor, perc: CGFloat) {
let center = CGPoint(x: 150, y:150)
let radius = min(200, 200) / 2
let startAngle = 0 / 100 * CGFloat(Double.pi) * 2 - CGFloat(Double.pi)
let endAngle = perc / 100 * CGFloat(Double.pi) * 2 - CGFloat(Double.pi)
let path = UIBezierPath()
path.move(to: center)
path.addArc(withCenter: center, radius: CGFloat(radius), startAngle: startAngle, endAngle: endAngle, clockwise: true)
colour.setStroke()
path.close()
path.lineWidth = 20
path.stroke()
}
let leftColour = UIColor.red
let middleColour = UIColor.yellow
let rightColour = …Run Code Online (Sandbox Code Playgroud) 在我的 iOS 应用程序中,我使用 SPM 来管理依赖项。其中之一是一个加载资源的库Bundle.module(Swift 5.3 中提供的一项新功能)。但现在我需要在我的应用程序中使用 CocoaPods。因此,我应该添加 CocoaPods 对这种依赖关系的支持。我知道我可以将Bundle.module的声明从resources_bundle_accessor.swift复制到库中。但它也应该支持SPM。我想知道如何检查是否应该使用在resource_bundle_accessor.swiftBundle.module中定义的函数,或者如果未生成resource_bundle_accessor.swift,则我的函数会执行相同的操作。