总得说点什么
Illusionary-Daytime
发表于
分类于
Music
不能断更,需要翻墙...
UserDefaultsWrapper
发表于
分类于
Swift
又水一篇...
Moya-HandyJSON
发表于
分类于
Swift
水一篇,好像以前不水一样...
逆波兰表达式求值
发表于
分类于
Algorithm
1 | // 计算逆波兰表达式 |
–EOF–
协议前缀
发表于
分类于
Swift
1 | import Foundation |
–EOF–
Vapor最简单的服务器
发表于
分类于
Vapor
Package.swift
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "HelloWorld",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-alpha.2"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "HelloWorld",
dependencies: ["Vapor"]),
.testTarget(
name: "HelloWorldTests",
dependencies: ["HelloWorld"]),
]
)
main.swift
import Vapor
let app = Application(environment: try .detect()) { (service) in
service.extend(Routes.self) { (routes, container) in
routes.get("hello") { (req) -> String in
"Hello World!"
}
}
}
try app.run()
–EOF–
Swift实现可选协议
发表于
分类于
Swift
Swift 属性绑定
发表于
分类于
Swift
class Person { }
extension Person {
private static var age_key = false //占一个字节,省空间
private static var weight_key = false //占一个字节,省空间
// private static var age_key1: Void? //这种方式也是可以的
// private static var weight_key1: Void?
var age: Int {
get {
objc_getAssociatedObject(self, &Self.age_key) as! Int
}
set {
// self abc 都相当于是key
objc_setAssociatedObject(self, &Self.age_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
}
}
var weight: Int {
get {
objc_getAssociatedObject(self, &Self.weight_key) as! Int
}
set {
// self abc 都相当于是key
objc_setAssociatedObject(self, &Self.weight_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
}
}
}
var p = Person()
p.age = 10
print(p.age)
p.weight = 50
print(p.weight)
–EOF–
Swift重命名C函数
发表于
分类于
Swift
Test.h
#ifndef Test_h
#define Test_h
#include <stdio.h>
int sum(int v1, int v2);
#endif /* Test_h */
Test.c
#include "Test.h"
int sum(int v1, int v2) {
return v1 + v2;
}
main.swift
// 重命名c函数
@_silgen_name("sum")
func swift_sum(_ v1: Int32, _ v2: Int32) -> Int32
print(swift_sum(1, 2))
–EOF–