除了应用程序的第一次初始安装之外,apple-app-site-association json文件是否会更新?
例如,如果我在json文件中为路径数组添加一个额外的值,我的应用程序是否会获得该更新?
我发现.contains(Element)方法在我编写Swift代码的最小经验中非常重要,并且很快意识到Apple改变了它...
func contains(check:[[[Int]]], forElement: [[Int]]) -> Bool {
for element in check {
if areEqual(element, forElement) {
return true
}
}
return false
}
func areEqual(_ a:[[Int]], _ b:[[Int]]) -> Bool {
for i in 0..<a.count {
if a[i] != b[i] {
return false
}
}
return true
}
Run Code Online (Sandbox Code Playgroud)
我一直在搞乱一些大型数组,所以我用那个笨重的函数解决了我的问题.
发生了什么?
你如何使用新的方式?
那里的例子很好,我确信他们打算这样做.
enum HTTPResponse {
case ok
case error(Int)
}
let lastThreeResponses: [HTTPResponse] = [.ok, .ok, .error(404)]
let hadError = lastThreeResponses.contains { element in
if case .error …Run Code Online (Sandbox Code Playgroud) 在Objective-C中,获取实例的类并在其上调用类方法很简单.你是如何在Swift 4中做到这一点的?
例如
MyObject * obj = [[MyObject alloc] init];
[[obj class] someClassMethod];
Run Code Online (Sandbox Code Playgroud) 这是我的课,它会画一个圆圈,它看起来像这样:
class OvalLayer: CAShapeLayer {
let animationDuration: CFTimeInterval = 0.3
override init() {
super.init()
fillColor = Colors.green.CGColor
path = ovalPathSmall.CGPath
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var ovalPathStart: UIBezierPath {
let path = UIBezierPath(ovalInRect: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
return path
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要在这个圈子的中间添加一个文本,我试图在谷歌上找到它,但没有任何工作正常.我不确定是否可能,如果有可能,有人可以帮助我吗?
从Swift 2.2转换为3.0后,我的Array扩展不再编译,因为它包含对全局标准库函数的调用min<T>(T,T)并显示编译器错误extra argument in call.
这是重现错误的简单方法:
extension Array {
func smallestInt(first: Int, second: Int) -> Int {
return min(first, second) // compiler error: "Extra argument in call"
}
}
Run Code Online (Sandbox Code Playgroud)
将相同的函数添加到扩展名时Dictionary,我得到相同的错误,而完全相同的代码在其他类型的扩展中编译得很好(例如String或AudioBuffer):
看着的文件Array和Dictionary,我发现有关于实例方法Sequence命名public func min() -> Element?和public func min(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Element?.虽然两者String并AudioBuffer没有任何形式的min(...)功能.
这可能是我无法调用全局函数的原因吗?编译器无法区分全局func min<T>(T,T),self.min(...)尽管它们具有完全不同的签名? …
我有一个带有1个可选字段和1个非可选字段的类,它们都具有Type AnotherClass并且还符合CustomProtocol:
protocol CustomProtocol {}
class CustomClass: CustomProtocol {
var nonoptionalField: AnotherClass = AnotherClass()
var optionalField: AnotherClass?
}
class AnotherClass: CustomProtocol {
}
Run Code Online (Sandbox Code Playgroud)
现场nonoptionalField是类型AnotherClass,符合CustomProtocol.
另一方面,optionalField实际上是Optional <AnotherClass>,因此不符合CustomProtocol:
for field in Mirror(reflecting: CustomClass()).children {
let fieldMirror = Mirror(reflecting: field.value)
if fieldMirror.subjectType is CustomProtocol.Type {
print("\(field.label!) is \(fieldMirror.subjectType) and conforms CustomProtocol")
} else {
print("\(field.label!) is \(fieldMirror.subjectType) and DOES NOT conform CustomProtocol")
}
}
// nonoptionalField is AnotherClass …Run Code Online (Sandbox Code Playgroud) 该filter方法是一个非常强大的工具,可以根据单个或多个条件进行过滤,但是有没有办法根据数组条件进行过滤?
类Car与属性:model,color,engineStatus.
cars 是一个几乎没有汽车的阵列一个条件看起来像:
let currModel = `Opel`
let filterdObject = cars.filter { $0.model == currModel }
Run Code Online (Sandbox Code Playgroud)
两个或多个条件看起来像:
let currModel = `Opel`
let currColor = `Green`
let filterdObject = cars.filter { $0.model == currModel || $0.color == currColor }
Run Code Online (Sandbox Code Playgroud)
我的问题是我怎么能用数组过滤:
阵列具有例如两种颜色blue和green.我想cars用这些颜色过滤.我的意思是得到一个公式n-conditions.
如果我有一个像...那样的通用结构
struct Blah<T> {
let someProperty: T
}
Run Code Online (Sandbox Code Playgroud)
然后我就可以扩展Blah到符合Equatable时才T是Equatable.喜欢...
extension Blah: Equatable where T: Equatable {
static func == (lhs: Blah, rhs: Blah) -> Bool {
return lhs.someProperty == rhs.someProperty
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我尝试了几种不同的编码方式,但每种方法都给我一个稍微不同的错误.
假设您(Int)->()在Swift 3中有两个类型的闭包,并测试它们是否彼此相同:
typealias Baz = (Int)->()
let closure1:Baz = { print("foo \($0)") }
let closure2:Baz = { print("bar \($0)") }
if(closure1 == closure2) {
print("equal")
}
Run Code Online (Sandbox Code Playgroud)
这无法编译,给出消息:
二进制运算符'=='不能应用于两个'(Int) - >()'操作数
好吧,那么,我们如何比较两个相同类型的闭包,看看它们是否相同?
我正在尝试向UIViewController添加其他属性.
码:
protocol AdditionalStoredProperties
{
associatedtype Title
func getAssociatedObject<Title>(key: UnsafePointer<Title> ,
defValue : Title)->Title
}
extension AdditionalStoredProperties
{
func getAssociatedObject<Title>( key: UnsafePointer<Title> , defValue : Title)->Title
{
guard let actual_value = objc_getAssociatedObject(self as! AnyObject, key) as? Title else
{
return defValue
}
return actual_value
}
}
extension UIViewController:AdditionalStoredProperties
{
typealias Title = String
var previousPage : String
{
get { return getAssociatedObject(&self.previousPage, defValue: self.previousPage) }
set { objc_setAssociatedObject(self, &self.previousPage, newValue, .OBJC_ASSOCIATION_RETAIN)}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
错误:尝试将堆栈放在不可读的内存中:
我知道我们不能直接将存储的属性添加到扩展,所以我尝试使用objc_setAssociatedObject()添加它