This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // macros that can be used to quickly check the model of current iPhone in your Objective-C code. | |
| #define IS_IPHONE_6_PLUS ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )736 ) < DBL_EPSILON ) | |
| #define IS_IPHONE_6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON ) | |
| #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) | |
| #define IS_IPHONE_4 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Let's explore a little bit how to break the strong reference cycles. | |
| * In this Playground file I re-created the examples mentioned in the ARC chapter in the Swift Porgramming Lang book: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html | |
| * In each section, the code is implemented to avoid strong reference cycle. If you want to see how a strong reference cycle can actually occur follow the comments in each section play for yourself. | |
| * Avoiding memory leak is important for making your app memory-efficient and, in some cases, removing the risks of crashing out. | |
| */ | |
| import UIKit | |
| /* Section 1 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Language { | |
| private let separator = ", " | |
| private var productions = [String: [String]]() | |
| init(setUpProductionRules: Bool) { | |
| if setUpProductionRules { | |
| self.setUpProductionRules() | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ParseTreeNode { | |
| let nonTerminal: String | |
| let leftNode: ParseTreeNode? | |
| let rightNode: ParseTreeNode? | |
| init(nonTerminal: String, leftNode: ParseTreeNode?, rightNode: ParseTreeNode?) { | |
| self.nonTerminal = nonTerminal | |
| self.leftNode = leftNode | |
| self.rightNode = rightNode | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CKYParser { | |
| func parse(words words: [String], language: Language) -> [[[ParseTreeNode]]] { | |
| let wordCount = words.count | |
| var table = [[[ParseTreeNode]]]() | |
| for _ in 0...words.count - 1 { | |
| let array = [[ParseTreeNode]](count: wordCount, repeatedValue: [ParseTreeNode]()) | |
| table.append(array) | |
| } | |
| // populate lexicon. assuming all the words are in our lexicon | |
| for index in 0...words.count - 1 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let wordCount = words.count | |
| var table = [[[ParseTreeNode]]]() | |
| for _ in 0...words.count - 1 { | |
| let array = [[ParseTreeNode]](count: wordCount, repeatedValue: [ParseTreeNode]()) | |
| table.append(array) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for index in 0...words.count - 1 { | |
| let word = words[index] | |
| let nonTerminals = language.recognize([word])! | |
| for nonTerminal in nonTerminals { | |
| table[index][index].append(ParseTreeNode(nonTerminal: nonTerminal, leftNode: nil, rightNode: nil)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for col in 0...wordCount - 1 { | |
| for row in (0...wordCount - 1).reverse() { | |
| var t = row | |
| while t + 1 <= col { | |
| let arr1 = table[row][t] | |
| let arr2 = table[t + 1][col] | |
| if arr1.count == 0 || arr2.count == 0 { | |
| t += 1 | |
| continue | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MessageViewModel { | |
| let messageSubject = BehaviorSubject<String>(value: "Greetings") | |
| func receiveMessage(message: String) { | |
| messageSubject.onNext(message) | |
| } | |
| } | |
| class MessageViewController: UIViewController { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| messageViewModel.messageSubject.subscribeNext { [unowned self] message in | |
| self.messageLabel.text = message | |
| }.addDisposableTo(disposeBag) |
OlderNewer