本文的目的是介绍Swift:通用类中的声明的详细情况,特别关注在swift中声明一个变量用什么关键字的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Swift:通用类
本文的目的是介绍Swift:通用类中的声明的详细情况,特别关注在swift中声明一个变量用什么关键字的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Swift:通用类中的声明的机会,同时也不会遗漏关于#学习笔记#Swift中Class的声明与运用、ios – swift:声明公共变量、ios – Swift:无法找到协议声明、ios – Swift:通过索引移动数组中的元素的知识。
本文目录一览:- Swift:通用类中的声明(在swift中声明一个变量用什么关键字)
- #学习笔记#Swift中Class的声明与运用
- ios – swift:声明公共变量
- ios – Swift:无法找到协议声明
- ios – Swift:通过索引移动数组中的元素
Swift:通用类中的声明(在swift中声明一个变量用什么关键字)
假设我有一个通用类:
class SomeClass<Element> { // What is the difference between this: var array: [SomeClass]! // and this declaration: var array2: [SomeClass<Element>]!}
这些声明之间有什么区别?
答案1
小编典典没有泛型参数就不能存在泛型类型,因此省略泛型类型意味着您希望编译器推断该类型。
在这种情况下,要推断的逻辑类型为Element
。
请注意,这仅适用SomeClass
于您使用了内部SomeClass
声明。它不适用于其他泛型类。
可以在变量声明中使用类似的通用参数推断:
let instance: SomeClass = SomeClass<Int>()let array: Array = [1] // Array<Int>
#学习笔记#Swift中Class的声明与运用
import Foundation
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() ->String {
return "A shape with \(numberOfSides) sides."
}
}
// 注意这里的对象初始,需要传递name的属性数值进去
// 初始的时候一定要写上“name:“,否则编译器会认为没有进行初始化工作
var shape = NamedShape(name: "Bob")
// 下面是继承类的声明与使用
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
// 由于name属性是通过父类初始函数初始化的
// 所以必须调用父类的init方法初始化name属性
super.init(name: name)
// 可以直接使用父类属性
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
// 重载父类方法,一定要写明override,否则编译器会报错
override func simpleDescription() ->String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "my test squre")
println(test.area())
println(test.simpleDescription())
以下是getter和setter的用法示例
class EquilateralTriangle: NamedShape {
var sideLength: Double = 0.0
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 3
}
var perimeter: Double {
get {
return 3.0 * sideLength
}
set {
sideLength = newValue / 3.0
}
}
override func simpleDescription() ->String {
return "An quilateral triagle with side of length \(sideLength)"
}
}
var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
println(triangle.perimeter)
triangle.perimeter = 9.9
println(triangle.sideLength)
以下是介绍类模块中willSet和didSet的用法,这部分比较特别
willSet可以在设置新的属性值之前运行,下面的示例是保证triangle和square的sideLength始终保持相等
class TriangleAndSquare {
var square: Square {
willSet {
triangle.sideLength = newValue.sideLength
}
}
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
}
}
init(size: Double, name: String) {
square = Square(sideLength: size, name: name)
triangle = EquilateralTriangle(sideLength: size, name: name)
}
}
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
println(triangleAndSquare.square.sideLength)
println(triangleAndSquare.triangle.sideLength)
// 如果是直接改变square中的sideLength的值,那么triangle中的sideLength是不会发生变化的
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
println(triangleAndSquare.triangle.sideLength)
类和结构体的主要区别是,结构体是值传递(依靠值的复制进行代码内部的传递),类是引用传递
ios – swift:声明公共变量
class XYActivity: UIActivity,YouTubeHelperDelegate { var youTubeHelper:YouTubeHelper var uploadURL: String! override init() { self.youTubeHelper = YouTubeHelper() } override func activityType() -> String? { return nil } // }
我想让uploadURL公开,也就是说,要在其他类中分配.
当我添加公共infront的var uploadURL:String!它建议我把它作为内部.我想让它公开.请帮忙
解决方法
默认情况下,修饰符是内部的,这使得当前模块中的任何地方的类,方法和属性未被显式声明为私有.
如果你的项目仅由一个应用程序组成,那么你可能不需要公共 – 内部的效果相同.如果您正在开发框架,并且需要该属性可以从其他模块中的代码访问,则需要将整个类和暴露的方法/属性声明为public.
建议阅读:Access Control
摘录描述默认访问级别:
All entities in your code (with a few specific exceptions,as described later in this chapter) have a default access level of internal if you do not specify an explicit access level yourself. As a result,in many cases you do not need to specify an explicit access level in your code.
以及单一目标应用的访问级别:
When you write a simple single-target app,the code in your app is typically self-contained within the app and does not need to be made available outside of the app’s module. The default access level of internal already matches this requirement. Therefore,you do not need to specify a custom access level. You may,however,want to mark some parts of your code as private in order to hide their implementation details from other code within the app’s module.
ios – Swift:无法找到协议声明
Cannot find protocol declaration for ‘EKEventEditViewDelegate’
Expected a type
错误显示在我的项目-swift.h中(该项目混合了Swift和Objective-c).
知道为什么会这样吗?除非我遗漏了一些东西,否则我看不到可能导致这种情况的任何基本错误(拼写错误等).
解决方法
ios – Swift:通过索引移动数组中的元素
var array = [1,2,3,4,5]
我可以写一个扩展到Array,所以我可以修改数组来实现这个输出:[2,5,1]:
mutating func shiftRight() { append(removeFirst()) }
有没有办法实现这样的功能,可以通过任何索引(正或负)来移动数组.我可以用if-else子句强制执行这个功能,但是我正在寻找的是功能实现.
算法很简单:
>按提供的索引将数组拆分成两个
>将第一个数组追加到第二个数组的末尾
有没有什么办法实现它的功能风格?
我完成的代码:
extension Array { mutating func shift(var amount: Int) { guard -count...count ~= amount else { return } if amount < 0 { amount += count } self = Array(self[amount ..< count] + self[0 ..< amount]) } }
解决方法
extension Array { func shiftRight(var amount: Int = 1) -> [Element] { assert(-count...count ~= amount,"Shift amount out of bounds") if amount < 0 { amount += count } // this needs to be >= 0 return Array(self[amount ..< count] + self[0 ..< amount]) } mutating func shiftRightInPlace(amount: Int = 1) { self = shiftRight(amount) } } Array(1...10).shiftRight() // [2,6,7,8,9,10,1] Array(1...10).shiftRight(7) // [8,1,7]
您也可以从shiftRight()返回Array(后缀(count – amount)前缀(amount))而不是下标.
关于Swift:通用类中的声明和在swift中声明一个变量用什么关键字的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于#学习笔记#Swift中Class的声明与运用、ios – swift:声明公共变量、ios – Swift:无法找到协议声明、ios – Swift:通过索引移动数组中的元素的相关知识,请在本站寻找。
本文标签: