@available with pre-available iOS versions - missing properties

时间:2015-06-30 13:57:32

标签: ios swift ios9

I've implemented such class:

class MapLayoutGuide: NSObject, UILayoutSupport {
    var insetLength: CGFloat = 0
    init(insetLength: CGFloat) {
        self.insetLength = insetLength
    }
    var length: CGFloat {
        return insetLength
    }
}

Everything was working fine, however there were new changes introduced with new iOS version: Apple changelog.

So now I'm receiving 3 errors:

  • Protocol requires property 'topAnchor' with type 'NSLayoutYAxisAnchor',
  • Protocol requires property 'bottomAnchor' with type 'NSLayoutYAxisAnchor',
  • Protocol requires property 'heightAnchor' with type 'NSLayoutDimension'.

Looking into UILayoutSupport implementation I can see new variables:

@available(iOS 9.0, *)
var topAnchor: NSLayoutYAxisAnchor { get }
@available(iOS 9.0, *)
var bottomAnchor: NSLayoutYAxisAnchor { get }
@available(iOS 9.0, *)
var heightAnchor: NSLayoutDimension { get }

My app is iOS 8.0+. So the question is what should I do with these values..? I can't set @available flag and I want the code to work both with iOS 8 and 9, but I have to override it. No conception what to do with it.

The code used to work yesterday on Xcode Beta 1, what ofc doesn't matter atm as I want it to work on current API not previous.

1 个答案:

答案 0 :(得分:7)

在清理项目后它起作用了。

@available(iOS 9.0, *)
var topAnchor: NSLayoutYAxisAnchor {
    return NSLayoutYAxisAnchor()
}


@available(iOS 9.0, *)
var bottomAnchor: NSLayoutYAxisAnchor {
    return NSLayoutYAxisAnchor()
}


@available(iOS 9.0, *)
var heightAnchor: NSLayoutDimension {
    return NSLayoutDimension()
}