Skip to content

Commit

Permalink
adopt swift5
Browse files Browse the repository at this point in the history
  • Loading branch information
SolaWing committed May 1, 2019
1 parent 7496dce commit 0a22cad
Show file tree
Hide file tree
Showing 6 changed files with 673 additions and 626 deletions.
1,157 changes: 582 additions & 575 deletions AutoLayoutVisualFormat.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -20,6 +20,20 @@
ReferencedContainer = "container:AutoLayoutVisualFormat.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "210A01911CC4CE710040DE1D"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:AutoLayoutVisualFormat.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand All @@ -28,7 +42,26 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "210A01911CC4CE710040DE1D"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:AutoLayoutVisualFormat.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "212A164D21D50CC7007DB60D"
BuildableName = "VFL.framework"
BlueprintName = "VFLStatic"
ReferencedContainer = "container:AutoLayoutVisualFormat.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion VFL.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'VFL'
s.version = '1.1.0'
s.version = '2.0.0'
s.summary = 'Powerful and Easy to use AutoLayout Visual Format Language'

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 1 addition & 1 deletion VFL/UIView+SWAutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public extension UIView {
func VFLConstraints(_ interpolation:VFLInterpolation) -> [NSLayoutConstraint] {
let format = NSMutableString()
let env = NSMutableArray(object: self)
interpolation.result(format, env)
interpolation.resultInto(format, env)
return VFL.VFLViewConstraints(format as String, self, env)
}

Expand Down
101 changes: 54 additions & 47 deletions VFL/VFLInterpolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func buildInterpolationResult(_ parts: [VFLInterpolation]) -> (format: String, e
let format = NSMutableString()
let env = NSMutableArray()
for part in parts {
part.result(format, env)
part.resultInto(format, env)
if !format.hasSuffix(";") {
format.append("; ")
}
Expand Down Expand Up @@ -59,63 +59,59 @@ public func fullInstall(_ interpolation: [VFLInterpolation]) -> [NSLayoutConstra
}

// MARK: -
public enum VFLInterpolation : ExpressibleByStringInterpolation, ExpressibleByStringLiteral {
public struct VFLInterpolation : ExpressibleByStringInterpolation, ExpressibleByStringLiteral {
public struct StringInterpolation: StringInterpolationProtocol {
public init(literalCapacity: Int, interpolationCount: Int) {
storage.reserveCapacity(interpolationCount * 2 + 1)
}
public init(string: String) {
storage.append(.format(string))
}

case format(String)
case metric(CGFloat)
case collection([VFLInterpolation])
case other(AnyObject)
public mutating func appendLiteral(_ literal: StringLiteralType) {
storage.append(.format(literal))
}
public mutating func appendInterpolation<T: BinaryFloatingPoint>(_ value: T) {
storage.append(.metric(CGFloat(value)))
}
public mutating func appendInterpolation<T: BinaryInteger>(_ value: T) {
storage.append(.metric(CGFloat(value)))
}
public mutating func appendInterpolation<T: AnyObject>(_ value: T) {
storage.append(.other(value))
}
public enum Parts {
/// literal string part
case format(String)
/// metrics part
case metric(CGFloat)
/// any constraint item type, like View or guide
case other(AnyObject)
}
var storage = [Parts]()
}
var parts: StringInterpolation

// MARK: ExpressibleByStringLiteral
public init(stringLiteral value: StringLiteralType){
self = .format(value)
parts = StringInterpolation(string: value)
}

public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterType){
self = .format(value)
parts = StringInterpolation(string: value)
}

public init(unicodeScalarLiteral value: UnicodeScalarType) {
self = .format(value)
parts = StringInterpolation(string: value)
}

// MARK: ExpressibleByStringInterpolation
public init(stringInterpolation strings: VFLInterpolation...) {
self = .collection(strings)
}

public init(stringInterpolationSegment str: String) {
self = .format(str)
}

public init(stringInterpolationSegment value: CGFloat) {
self = .metric(value)
public init(stringInterpolation: StringInterpolation) {
parts = stringInterpolation
}

public init<T>(stringInterpolationSegment expr: T) {
self = .other( expr as AnyObject )
}

func result() -> (format: String, env: [AnyObject])! {
switch self {
case .collection(let parts):
let env = NSMutableArray(capacity: parts.count)
let format = NSMutableString()
for part in parts {
part.result(format, env)
}
return (format as String, env as [AnyObject])
case .format(let format):
return (format, [])
default:
assertionFailure("call result on incomplete type")
return nil
}
}

/** fill format and env acording to self part */
func result(_ format: NSMutableString, _ env: NSMutableArray) {
switch self {
func fillResult(format: NSMutableString, env: NSMutableArray, part: StringInterpolation.Parts) {
switch part {
case .format(let str):
format.append(str)
case .metric(let value):
Expand All @@ -124,10 +120,21 @@ public enum VFLInterpolation : ExpressibleByStringInterpolation, ExpressibleBySt
case .other(let obj):
format.appendFormat("$%u", env.count)
env.add(obj)
case .collection(let parts): // recursive add format and env
for part in parts {
part.result(format, env)
}
}
}

func result() -> (format: String, env: [AnyObject])! {
let parts = self.parts.storage
let env = NSMutableArray(capacity: parts.count)
let format = NSMutableString()
resultInto(format, env)
return (format as String, env as [AnyObject])
}

/// fill format and env acording to self part
func resultInto(_ format: NSMutableString, _ env: NSMutableArray) {
parts.storage.forEach {
fillResult(format: format, env: env, part: $0)
}
}
}

2 comments on commit 0a22cad

@wanggshuo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

惊了,竟然还更新的

@SolaWing
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

功能正常还是要保证的。更新得其实有些慢了

Please sign in to comment.