//swift 暴露 objc 调用
@objc(NSCar)
@objcMembers class Car: NSObject {
//Mark: - 属性
var price: Double
@objc(name)
var band: String
@objc init(price: Double, band: String) {
self.price = price
self.band = band
}
func run() { print(price, band, "run") }
@objc(drive)
static func run() { print("Car run") }
}
extension Car {
func test() { print(price, band, "test") }
}
//swift 调用 objc
var pn = NSPerson(age: 29, name: "Jack")
pn.run();
pn.eat("APPle", other: "balana")
pn.age = 30
pn.name = "Rose"
pn.eat("APPles", other: "balana")
NSPerson.run();
NSPerson.eat("面包", other: "水果")
func sum(_ a: Int, _ b: Int) -> Int { a - b }
@_silgen_name("sum") //修改c语言函数名
func swift_sum(_ v1: Int32,
_ v2: Int32) -> Int32
print(swift_sum(89, 11))
print(sum(89, 11))
//自定义表达式模式 (重载运算符 ~=,自定义匹配规则)
extension String{
static func ~= (pattern: (String) -> Bool, value: String) -> Bool {
pattern(value)
}
}
func hasPrefix(_ prefix: String) -> ((String) -> Bool) {
// return {
// (str: String) -> Bool in
// str.hasPrefix(prefix)
// }
{ $0.hasPrefix(prefix) }
}
func hasSuffix(_ suffix: String) -> ((String) -> Bool) { { $0.hasSuffix(suffix) } }
var str = "123456"
switch str {
case hasPrefix("23"):
print("以123开头")
case hasSuffix("56"):
print("以567开头")
default:
break
}
extension Int {
static func ~= (pattern: (Int) -> Bool, value: Int) -> Bool {
pattern(value)
}
}
func isEven(_ i: Int) -> Bool { i % 2 == 0 }
func isOdd(_ i: Int) -> Bool { i % 2 != 0 }
var num = 5
switch num {
case let aa where aa > 5:
print(aa)
case isEven:
print("偶数")
case isOdd:
print("奇数")
default:
break
}
var fn = hasPrefix("21")
print(fn("123"))
let a: Int? = 10
if case .some(let b) = a {
print(b)
}
let ages: [Int?] = [nil, 2, 3]
for case .some(let age) in ages {
print(age)
}
class Student: ExpressibleByStringLiteral {
var name: String = ""
required init(stringLiteral value: String) {
self.name = value
}
}
var stu: Student = "jack"
print(stu.name)
var string = "1-2-3-4-5-6jklllllllllllllldsfasfdabcdefghijk"
//string.insert("&", at: string.startIndex)
//string.insert(contentsOf: "___", at: string.index(after: string.startIndex))
//string.insert(contentsOf: "^^^", at: string.index(string.endIndex, offsetBy: -1))
//string.remove(at: string.index(string.endIndex, offsetBy: -1))
string.remove(at: string.firstIndex(of: "2")!)
string.removeAll {
$0 == "5"
}
var range = string.index(string.endIndex, offsetBy: -4)...string.index(before: string.endIndex)
var str11 = string[range]
string.removeSubrange(range)
//var subString = string.appendingFormat("%@", "hh")
//var subString = string.index(after: string.endIndex-1)
var subStr = string.suffix(3)
var subStr1 = subStr.prefix(2)
var ptr88 = withUnsafeMutablePointer(to: &string) { UnsafeMutableRawPointer($0) }
string.append("-7")
ptr88 = withUnsafeMutablePointer(to: &string) { UnsafeMutableRawPointer($0) }
print(subStr);
协议protocol extension
class Stack<E> {
var elements = [E]()
func push(_ element: E) {
elements.append(element)
}
func pop() -> E { elements.removeLast() }
func size() -> Int { elements.count }
}
extension Stack{
func top() -> E { elements.last! }
}
extension Stack : Equatable where E : Equatable {
static func == (left: Stack, right: Stack) -> Bool {
left.elements == right.elements
}
}
protocol TestProtocol {
func test1()
}
extension TestProtocol {
func test1(){
print("TestProtocol 1")
}
func test2() {
print("TestProtocol 2")
var a = 1
assert(a == 2,"kjflsd")
}
}
class testClass : TestProtocol, CustomStringConvertible, CustomDebugStringConvertible {
func test1(){
print("testClass 1")
}
func test2() {
print("testClass 2")
}
var age = 10
var description: String { "test_\(age)" }
var debugDescription: String { "debug_test_\(age)" }
}
var cls: TestProtocol = testClass()
cls.test1()
cls.test2()
//print(cls.debugDescription)
extension
func isOdd<T : BinaryInteger>(_ num: T) -> Bool {
num % 2 != 0
}
extension BinaryInteger{
func isOdd() -> Bool { self % 2 != 0 }
}
extension Int{
func repeats(task:() -> Void){
for _ in 0 ..< self { task() }
}
enum Kind {case negative, zero, positive }
var kind : Kind {
switch self {
case 0:
return .zero
case let x where x > 0:
return .positive
default:
return .negative
}
}
subscript(index: Int) -> Int{
let s = String(self)
guard (index < s.count && index >= 0) else { return 0 }
var decimalBase = 1
for _ in 0 ..< index {
decimalBase *= 10
}
return (self / decimalBase) % 10
}
}
var age = 78943
print(age[-1])
5.repeats {
print(2.kind)
}
var arr: Array<Int> = [10, 20, 30]
extension Array {
subscript(nullable idx: Int) -> Element? {
if (startIndex ..< endIndex).contains(idx) {
return self[idx];
}
return nil;
}
}
print(arr[nullable: 3] ?? 0);