我从Swift编译器('A' is not convertible to 'A','E' is not identical to 'E')中遇到了一些令人困惑的错误消息,我认为这是因为我在两个作用域中引入了同名的类型变量,其中一个作用域嵌套在另一个作用域中。
我想更完整地了解类型变量是如何工作的,所以我有几个相关的问题:
Swift中类型变量的范围是什么?
现在,嵌套类型和方法是否会影响范围?
类型变量可以隐藏吗?
当嵌套方法引入与封闭类同名的类型变量时,会发生什么情况?
是否可以做类似于method2的事情(
如何表示一个Swift泛型函数,该函数将T限制为Optional<Equatable>
我尝试了下面这样的方法,以及由此产生的错误。
func gen<T>(arg: T) where T : Optional<Equatable>
类型“T”受限于非协议类型“可选”
func gen<T>(arg: T) where T : OptionalProtocol<Equatable>
无法专门化非泛型类型“OptionalProtocol”
func gen<T, U>(arg: T) where T
为什么在Swift 3中使用字符串插值时,隐式地展开选项而不是展开?
示例:在操场上运行以下代码
var str: String!
str = "Hello"
print("The following should not be printed as an optional: \(str)")
产生这个输出:
The following should not be printed as an optional: Optional("Hello")
当然,我可以将字符串与+操作符连接起来,但是我几乎在我的应用程序中到处使用字符串插值,现在由于这个
如果我想让一个变量成为可选的,我可以这样做:
var exampleString: String? = nil
或
var exampleString: String? = "This is a string"
是否可以声明可选值而不指定类型,如String、Int、Bool等?
类似于(它不会返回错误):
var exampleVar? = nil
我没有为什么要这样做的用例,但我正在努力更好地理解optionals。我刚刚通读了Swift文档中的Optionals部分。如果可以做到这一点,这样做有什么好处吗?
还有,为什么我不想在任何地方都使用隐式展开的optionals?
我正在尝试在swift中设置一个泛型方法,其中的参数可以是[String:Any]?的字典或字典[[String:Any]]?的数组,两者都是可选的。我不熟悉Swift中这种级别的泛型语法,但我尝试过: private class func weirdGenerics<T>(thing:T) where T:Optional<[(String, Any)]>, T:Optional<[[(String, Any)]]>{
} 这给了我一个错误: Type 'T' Constrained to non-protocol, non-c
当我将枚举添加到泛型类时,为什么会出现错误:
class TestClass<T>{
enum TestEnum {
case test
}
}
错误:
1. While type-checking 'ExampleTest' at /Users/xxx/xxx/xx/xx/ExampleTest.swift:11:1
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swi
这是我在StackOverflow上找到的代码的一部分。
它是在Swift 1.2中工作的,为什么这段代码在swift 2中不再工作了:
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
let placeArray = placemarks as [CLPlacemark] // !!! ERROR HERE !!!
// Place details
var placeMark: CLPlacemar
我刚开始学习Swift4。我已经阅读了关于堆栈溢出重复问题的所有建议,但我无法解决或理解我的问题。
我从SQLite.swift数据库中获取数据:
ViewController.swift:
import Foundation
import SQLite
// Load DB with SQLite.swift
let path = Bundle.main.path(forResource: "myDataBaseFile", ofType: "sqlite")!
let db = try! Connection(path, readonly: true)
我尝试将带有type参数的类的对象转换为具有更通用类型参数的同一类的对象,如以下Swift代码所示:
class ParameterClassA {
}
class ParameterClassB: ParameterClassA {
}
class WorkingClassA<T> {
}
class WorkingClassB: WorkingClassA<ParameterClassB> {
}
let object1 = WorkingClassB()
let object2: WorkingClassA<ParameterClassA> =
比如。在新推出的语言中,Swift可以做C#中匿名类型可以做的事情吗?
在学习swift时,我发现了一些语句(他们称之为元组),比如
let http200Status = (statusCode: 200, description: "OK")
我们可以像这样访问它们
println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is \(http200Sta
我发现这个链接()解决了我的一个问题,但现在的问题是,当我试图使用嵌套的泛型继承UIView (在本例中)时,编译器崩溃。
class BaseModel {}
class BaseVM<T: BaseModel> {}
class BaseService<T: BaseModel, U: BaseVM<T>> : UIView {
//init viewmodel with generic model
}
class Human : BaseModel {
var name = ""
}
class HumanVM: B
为什么不是原始类型,泛型类中的静态变量?
例如,
public class MyType<E> {
class Inner { }
static class Nested { }
public static void main(String[] args) {
MyType mt; // warning: MyType is a raw type
MyType.Inner inn; // warning: MyType.Inner is a raw type
MyType.Nest
我有一个抽象 class是这样定义的:
class BaseCoordinator<ResultType>
其他类继承了这个类。
final class AppCoordinator: BaseCoordinator<Void>
final class AuthFlowCoordinator: BaseCoordinator<AuthFlowCoordinationResult>
final class MainFlowCoordinator: BaseCoordinator<Void>
现在,我想创建一个工厂方法。我想它的特征应该是:
func
namespace GenericsTest
{
public class AGenericClass<T>
{
public class NestedNonGenericClass
{
}
}
}
在上面的示例中,NestedNonGenericClass是否应该被视为泛型类?
反射API说它是一个泛型类,甚至还将包含类的模板参数作为嵌套类的模板参数传递给我。
Type nestedClass = typeof(AGenericClass<int>.NestedNonGenericClass);
我在苹果网站上阅读了swift文档。有一个函数swapTwoValues,它交换两个任意给定值
func swapTwoValues1<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
现在,我想编写类似的函数,但不是使用T泛型类型,而是使用任何
func swapTwoValues2(_ a: inout Any, _ b: inout Any) {
let temporaryA = a
a = b
b = temporaryA
}
我必须把下面的目标-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