diff --git a/Extras/README.md b/Extras/README.md new file mode 100644 index 0000000..2d23d2c --- /dev/null +++ b/Extras/README.md @@ -0,0 +1,4 @@ +# Extras +Creating another folder as to hold extra cool code snippets and tricks that help us fellow swift coders easily and quickly get on track in solving problems and building apps in an instant. +## Links +* [String<->Int Operations](https://github.com/TheNova22/learn-swift/blob/master/Extras/String%3C-%3EInt%20Operations.swift) diff --git a/Extras/String<->Int Operations.swift b/Extras/String<->Int Operations.swift new file mode 100644 index 0000000..035e3ae --- /dev/null +++ b/Extras/String<->Int Operations.swift @@ -0,0 +1,33 @@ +import UIKit +//Converts Strings into respective Data type and requirement +extension String { + // a -> 10 + var hexToInt : Int{return Int(strtoul(self, nil, 16))} + // a -> 10.0 + var hexToDouble : Double{return Double(strtoul(self, nil, 16))} + // a -> 1010 + var hexToBin : String{return String(hexToInt, radix: 2)} + // 10 -> a + var intToHex : String{ return String(Int(self) ?? 0, radix: 16)} + // 10 -> 1010 + var intToBin : String{return String(Int(self) ?? 0, radix: 2)} + // 1010 -> 10 + var binToInt : Int{return Int(strtoul(self, nil, 2))} + // 1010 -> 10.0 + var binToDouble : Double{return Double(strtoul(self, nil, 2))} + // 1010 -> a + var binToHexa : String{return String(binToInt, radix: 16)} + // a -> 97 + var ascii : Int{return Int(self.unicodeScalars.first!.value)} +} +//Converts Int into respective Data type and requirement +extension Int { + // 10 -> 1010 + var binString : String{return String(self, radix: 2)} + // 10 -> a + var hexString : String{return String(self, radix: 16)} + // 10 -> 10.0 + var double : Double{ return Double(self)} + // 97 -> a + var intToAscii : String{return String(Character(UnicodeScalar(self)!))} +} diff --git a/README.md b/README.md index e5ddcf3..2be74c3 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,16 @@ learn-swift Learn Apple's Swift programming language interactively through these playgrounds. -###Target Audience +### Target Audience Programmers familiar with C++ and/or Objective-C. -###What you'll need +### What you'll need You will need XCode 6.0 GM (or later) or 6.1 Beta 2 (or later) and probably a Mac to run it on. -###Purpose & Goal +### Purpose & Goal More than a primer, these playgrounds are intended to get programmers up to speed on Swift as fast as possible so they can begin using Swift productively. @@ -24,7 +24,7 @@ Learn Apple's Swift programming language interactively through these playgrounds along the way. Play the 'what-if' game. These are live playgrounds which offer near-realtime feedback of edits. -###Source of Content +### Source of Content I created these while working my way through the "Language Guide" section of Apple's book, "The Swift Programming Language". I feel the information from @@ -35,7 +35,7 @@ Learn Apple's Swift programming language interactively through these playgrounds If you don't already have the book, it's free. You should probably get it. -###Contributors +### Contributors Thanks to Rafał Wójcik for his quick work to update these playgrounds to incorporate the Swift language changes that came with XCode Beta 3.