Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-cain13 committed Jul 17, 2015
2 parents ea005c8 + d8c8e39 commit 11087ed
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
4 changes: 2 additions & 2 deletions R.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "R.swift"
s.version = "0.8.1"
s.version = "0.8.2"
s.summary = "Use strong typed, autocompleted resources like images and segues in Swift"

s.description = <<-DESC
Expand All @@ -22,7 +22,7 @@ Pod::Spec.new do |s|

s.platform = :ios, "7.0"

s.source = { :http => "https://github.com/mac-cain13/R.swift/releases/download/v0.8.1/rswift-0.8.1.zip" }
s.source = { :http => "https://github.com/mac-cain13/R.swift/releases/download/v0.8.2/rswift-0.8.2.zip" }

s.preserve_paths = "rswift"

Expand Down
10 changes: 6 additions & 4 deletions R.swift/func.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ func storyboardStructForStoryboard(storyboard: Storyboard) -> Struct {

let validateImagesLines = distinct(storyboard.usedImageIdentifiers)
.map { "assert(UIImage(named: \"\($0)\") != nil, \"[R.swift] Image named '\($0)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\")" }
let validateImagesFunc = Function(isStatic: true, name: "validateImages", parameters: [], returnType: Type._Void, body: join("\n", validateImagesLines))
let validateImagesFunc = Function(isStatic: true, name: "validateImages", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", validateImagesLines))

let validateViewControllersLines = catOptionals(storyboard.viewControllers
.map { vc in
vc.storyboardIdentifier.map {
"assert(\(sanitizedSwiftName($0)) != nil, \"[R.swift] ViewController with identifier '\(sanitizedSwiftName($0))' could not be loaded from storyboard '\(storyboard.name)' as '\(vc.type)'.\")"
}
})
let validateViewControllersFunc = Function(isStatic: true, name: "validateViewControllers", parameters: [], returnType: Type._Void, body: join("\n", validateViewControllersLines))
let validateViewControllersFunc = Function(isStatic: true, name: "validateViewControllers", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", validateViewControllersLines))

return Struct(type: Type(name: sanitizedSwiftName(storyboard.name)), lets: [], vars: instanceVars + initialViewControllerVar + viewControllerVars, functions: [validateImagesFunc, validateViewControllersFunc], structs: [])
}
Expand Down Expand Up @@ -168,6 +168,7 @@ func nibStructForNib(nib: Nib) -> Struct {
let instantiateFunc = Function(
isStatic: false,
name: "instantiateWithOwner",
generics: nil,
parameters: instantiateParameters,
returnType: Type(name: "[AnyObject]"),
body: "return instance.instantiateWithOwner(ownerOrNil, options: optionsOrNil)"
Expand All @@ -179,9 +180,10 @@ func nibStructForNib(nib: Nib) -> Struct {
Function(
isStatic: false,
name: "\($0.ordinal.word)View",
generics: nil,
parameters: instantiateParameters,
returnType: $0.view.asOptional(),
body: "return \(instantiateFunc.swiftName)(ownerOrNil, options: optionsOrNil)[\($0.ordinal.number - 1)] as? \($0.view)"
body: "return \(instantiateFunc.callName)(ownerOrNil, options: optionsOrNil)[\($0.ordinal.number - 1)] as? \($0.view)"
)
}

Expand Down Expand Up @@ -231,7 +233,7 @@ func varFromReusable(reusable: Reusable) -> Var {
// Validation

func validateAllFunctionWithStoryboards(storyboards: [Storyboard]) -> Function {
return Function(isStatic: true, name: "validate", parameters: [], returnType: Type._Void, body: join("\n", storyboards.map(swiftCallStoryboardValidators)))
return Function(isStatic: true, name: "validate", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", storyboards.map(swiftCallStoryboardValidators)))
}

func swiftCallStoryboardValidators(storyboard: Storyboard) -> String {
Expand Down
6 changes: 4 additions & 2 deletions R.swift/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,21 @@ struct Let: Printable {
struct Function: Printable {
let isStatic: Bool
let name: String
let generics: String?
let parameters: [Parameter]
let returnType: Type
let body: String

var swiftName: String {
var callName: String {
return sanitizedSwiftName(name, lowercaseFirstCharacter: true)
}

var description: String {
let staticString = isStatic ? "static " : ""
let genericsString = generics.map { "<\($0)>" } ?? ""
let parameterString = join(", ", parameters)
let returnString = Type._Void == returnType ? "" : " -> \(returnType)"
return "\(staticString)func \(swiftName)(\(parameterString))\(returnString) {\n\(indent(body))\n}"
return "\(staticString)func \(callName)\(genericsString)(\(parameterString))\(returnString) {\n\(indent(body))\n}"
}

struct Parameter: Printable {
Expand Down
36 changes: 24 additions & 12 deletions R.swift/values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ let ReuseIdentifierUITableViewExtension = Extension(
functions: [
Function(
isStatic: false,
name: "dequeueReusableCellWithIdentifier<T : \(Type._UITableViewCell)>",
name: "dequeueReusableCellWithIdentifier",
generics: "T : \(Type._UITableViewCell)",
parameters: [
Function.Parameter(name: "identifier", type: ReuseIdentifier.type),
Function.Parameter(name: "forIndexPath", localName: "indexPath", type: Type._NSIndexPath.asOptional(), defaultValue: "nil")
Expand All @@ -72,7 +73,8 @@ let ReuseIdentifierUITableViewExtension = Extension(

Function(
isStatic: false,
name: "dequeueReusableCellWithIdentifier<T : \(Type._UITableViewCell)>",
name: "dequeueReusableCellWithIdentifier",
generics: "T : \(Type._UITableViewCell)",
parameters: [
Function.Parameter(name: "identifier", type: ReuseIdentifier.type),
],
Expand All @@ -82,7 +84,8 @@ let ReuseIdentifierUITableViewExtension = Extension(

Function(
isStatic: false,
name: "dequeueReusableHeaderFooterViewWithIdentifier<T : \(Type._UITableViewHeaderFooterView)>",
name: "dequeueReusableHeaderFooterViewWithIdentifier",
generics: "T : \(Type._UITableViewHeaderFooterView)",
parameters: [
Function.Parameter(name: "identifier", type: ReuseIdentifier.type),
],
Expand All @@ -92,7 +95,8 @@ let ReuseIdentifierUITableViewExtension = Extension(

Function(
isStatic: false,
name: "registerNib<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell>",
name: "registerNib",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell",
parameters: [
Function.Parameter(name: "nibResource", type: Type(name: "T"))
],
Expand All @@ -102,7 +106,8 @@ let ReuseIdentifierUITableViewExtension = Extension(

Function(
isStatic: false,
name: "registerNibs<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell>",
name: "registerNibs",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell",
parameters: [
Function.Parameter(name: "nibResources", type: Type(name: "[T]"))
],
Expand All @@ -112,7 +117,8 @@ let ReuseIdentifierUITableViewExtension = Extension(

Function(
isStatic: false,
name: "registerNibForHeaderFooterView<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UIView>",
name: "registerNibForHeaderFooterView",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UIView",
parameters: [
Function.Parameter(name: "nibResource", type: Type(name: "T"))
],
Expand All @@ -127,7 +133,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(
functions: [
Function(
isStatic: false,
name: "dequeueReusableCellWithReuseIdentifier<T : \(Type._UICollectionViewCell)>",
name: "dequeueReusableCellWithReuseIdentifier",
generics: "T: \(Type._UICollectionViewCell)",
parameters: [
Function.Parameter(name: "identifier", type: ReuseIdentifier.type),
Function.Parameter(name: "forIndexPath", localName: "indexPath", type: Type._NSIndexPath)
Expand All @@ -138,7 +145,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(

Function(
isStatic: false,
name: "dequeueReusableSupplementaryViewOfKind<T : \(Type._UICollectionReusableView)>",
name: "dequeueReusableSupplementaryViewOfKind",
generics: "T: \(Type._UICollectionReusableView)",
parameters: [
Function.Parameter(name: "elementKind", type: Type._String),
Function.Parameter(name: "withReuseIdentifier", localName: "identifier", type: ReuseIdentifier.type),
Expand All @@ -150,7 +158,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(

Function(
isStatic: false,
name: "registerNib<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell>",
name: "registerNib",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell",
parameters: [
Function.Parameter(name: "nibResource", type: Type(name: "T"))
],
Expand All @@ -160,7 +169,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(

Function(
isStatic: false,
name: "registerNibs<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell>",
name: "registerNibs",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell",
parameters: [
Function.Parameter(name: "nibResources", type: Type(name: "[T]"))
],
Expand All @@ -170,7 +180,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(

Function(
isStatic: false,
name: "registerNib<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView>",
name: "registerNib",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView",
parameters: [
Function.Parameter(name: "nibResource", type: Type(name: "T")),
Function.Parameter(name: "forSupplementaryViewOfKind", localName: "kind", type: Type._String)
Expand All @@ -181,7 +192,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension(

Function(
isStatic: false,
name: "registerNibs<T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView>",
name: "registerNibs",
generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView",
parameters: [
Function.Parameter(name: "nibResources", type: Type(name: "[T]"))
],
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ _Tool to get strong typed, autocompleted resources like images, cells and segues

## Why use this?

You currently type:
It makes your code that uses resources:
- **Fully typed**, less casting and guessing what a method will return
- **Compiletime checked**, no more incorrect strings that make your app crash on runtime
- **Autocompleted**, never have to guess that image name again

Currently you type:
```swift
let icon = UIImage(names: "settings-icon")
let cell = dequeueReusableCellWithReuseIdentifier("textCell", forIndexPath: indexPath) as? TextCell
Expand All @@ -12,16 +17,11 @@ performSegueWithIdentifier("openSettings")

With R.swift it becomes:
```swift
let icon = R.images.settingsIcon
let icon = R.image.settingsIcon
let cell = dequeueReusableCellWithReuseIdentifier(R.reuseIdentifier.textCell, forIndexPath: indexPath)
performSegueWithIdentifier(R.segue.openSettings)
```

It makes your code that uses resources:
- **Fully typed**, less casting and guessing what a method will return
- **Compiletime checked**, no more incorrect strings that make your app crash on runtime
- **Autocompleted**, never have to guess that image name again

## Usage

After installing R.swift into your project you can use the `R`-struct to access resources. If the struct is outdated just build and R.swift will correct any missing/changed/added resources.
Expand Down

0 comments on commit 11087ed

Please sign in to comment.