如果我想让一个变量成为可选的,我可以这样做:
var exampleString: String? = nil
或
var exampleString: String? = "This is a string"
是否可以声明可选值而不指定类型,如String、Int、Bool等?
类似于(它不会返回错误):
var exampleVar? = nil
我没有为什么要这样做的用例,但我正在努力更好地理解optionals。我刚刚通读了Swift文档中的Optionals部分。如果可以做到这一点,这样做有什么好处吗?
还有,为什么我不想在任何地方都使用隐式展开的optionals?
我正在尝试定义一个扩展协议A的协议B(后者包含相关的类型C),同时按照编译器的建议使用带有相同类型约束的where子句。然而,当这样做的时候,代码将不再编译。这是一个Swift bug (在本例中是Swift 4)吗?
更具体地说,下面的代码编译时没有出现错误:
类型“E”不符合协议“A”
class D {
}
protocol A: class {
associatedtype C: AnyObject
}
protocol B: A where C == D {
}
class E: B {
}
如下所示更改协议B的定义将进行编译,但将显示此警告:
Typealias
我想知道swift.Following函数中的函数和泛型函数之间的区别,泛型函数正在执行same..can,有人告诉我泛型函数的确切用法吗?
func simpleMin<T: Comparable>(x: T, y: T) -> T { //Generic functions
if x < y {
return y
}
return x
}
func sampleMin(x:AnyObject,y:AnyObject)->AnyObject{ //Function
if x.integerValue <
这是我在util文件中的职责。它抱怨“'UIViewController‘类型的值没有成员'mapView'”
func removeSpecificCustomAnnotation(annotationArray: [CustomPointAnnotation], annotationIdToRemove: String, viewController: UIViewController) {
for annotation in annotationArray {
if let ID = annotation.locationID, ID ==
我必须把下面的目标-c代码翻译成斯威夫特。这是Objective 中的一个示例,其中框架提供的Optional协议应用于NSString类型的实例变量。我找到了一个,但没有成功。使用我的MYModel.swift实现,Xcode会抱怨Cannot specialize non-generic type NSString
谢谢你的帮助!
MYModel.swift
@objc(MYModel) public class MYModel : JSONModel {
...
public var name : NSString<Optional>
...
}
MYMod
我是swift的新手,我想扩展NSCoding以符合URL类型。我需要定义一个输入和输出类型,并且总是得到错误Type 'URL' does not conform to protocol 'NSCoding'。
//what it looks like before entering types
DiskCacheLevel<<#K: StringConvertible#>, NSCoding>()
//what I want it to look like (<InputType, OutputType>)
DiskCa
如何使用自定义函数utils扩展Swift的Array<T>或T[]类型?
浏览一下Swift的API文档,就会发现数组方法是T[]的扩展,例如:
extension T[] : ArrayType {
//...
init()
var count: Int { get }
var capacity: Int { get }
var isEmpty: Bool { get }
func copy() -> T[]
}
当复制和粘贴相同的源代码并尝试任何变体时,例如:
extension T[] : ArrayType {
我有一个使用类型的类,但是它不需要知道任何关于该类型的信息。在Swift中,我可以将其编码为两种方式之一:
protocol Type { }
class Class {
// use Type
}
或
class Class<Type> {
// use Type
}
现在,我倾向于后者,因为如果客户端想要为该类型使用一个空协议,他们可以使用一个空协议实例化泛型类。
我想了解别人的想法。
下面的代码用于我在SWIFT3.2中工作,但是随着Swift 4的最新发布,我得到了一个奇怪的错误,我无法理解。
我正在尝试创建一个类似这样的通用协议:
public protocol FactoryComponent {
associatedtype Argument
associatedtype Service
static var factory: (Resolver) -> (Argument) -> Service { get }
}
public extension FactoryComponent {
public typealias Facto
我有以下代码:
func roughlyEq<T: FloatingPoint>(_ a: T, _ b: T) -> Bool {
return abs(a - b) < (0.01 as! T)
}
当我使用浮动类型的参数调用它时,在运行时会发生崩溃:
Could not cast value of type 'Swift.Double' (0x10043e6a8) to 'Swift.Float' (0x10043e5e0)
这是没有意义的-- 0.01确实可以转换成浮点,比如当我编写Float(0.01)时。为什么会发生这个错
在移动开发中,接口和委托实现如下(以位置服务为例)
1. Android,Java
public class myClass implements LocationListener {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
在Swift中定义类时,可以具有var属性,这些属性与其他OOP语言中的正常字段类似,但也可以具有let属性,这些属性既是只读的,也是不可变的(如C++中的T const * const )。
然而,是否存在与C++的T * const等价的Swift?(也就是说,字段本身是不可变的,但它指向的对象是可变的)?
下面是我的场景的一个表示:
class Foo {
let bar: Bar
init(bar: Bar) {
self.bar = bar
}
}
protocol Bar {
var fleem: Int? { get set }
在下面的代码中,我为简单图项目中的顶点和边设置了协议。顶点引用边,边引用顶点。在neighbours()函数中,我在edges数组上调用map(),并得到以下错误:
“‘map”生成'T',而不是预期的上下文结果类型'Self.VertexType’。
import Foundation
protocol EdgeProtocol {
associatedtype VertexType: VertexProtocol
var from : VertexType { get set }
var to : VertexType { get
我有一个使用库的项目,我试图从两个视图控制器中提取一些逻辑。两个视图控制器都符合NVActivityIndicatorViewable,其定义为:
// From the libaray. I don't want to modify this.
public protocol NVActivityIndicatorViewable {}
public extension NVActivityIndicatorViewable where Self: UIViewController
{
func startAnimating( ... ) { ... }
func
我想做这样的事。
let float1: Float = 1.0
let float2: Float = 2.0
let float3 = f1.add(f2) // Float of value 3.0
let cgFloat1: CGFloat = 1.0
let cgFloat2: CGFloat = 2.0
let cgFloat3 = cgFloat1.add(cgFloat2) // CGFloat of value 3.0
let number1 = NSNumber(float: 1.0)
let number2 = NSNumber(float: 2.0)
let num
使用Swift 4.1 (Xcode 9.3),我尝试创建一组Int,但是我得到:
类型'Int‘不符合协议'Hashable’
但据我所知,说,只要数组的值符合哈斯可,它现在就符合哈斯可。作为一个解决办法,我有:
extension Array: Hashable where Element == Int {
public var hashValue: Int {
return debugDescription.hashValue
}
}
但我还是想知道为什么我不能免费得到哈斯。
我是程序初学者,请原谅这个天真的问题。Swift是我的第一语言,我没有引用(其他编程语言)来比较这种类型约束对协议扩展的好处。
就我个人而言,我只是感到困惑和奇怪。为什么存在对协议扩展的Type约束?它的意义是什么?
protocol TeamRecord {
var wins: Int { get }
var losses: Int { get }
func winningPercentage() -> Double
}
//Constraint:
extension CustomStringConvertible where Self: TeamRecord {
v