Skip to content

Commit c9ec24a

Browse files
committed
Updated to Swift 4
1 parent b03bbd5 commit c9ec24a

21 files changed

+45
-38
lines changed

Readme.md

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Xcode Swift Code Snippets
22

3-
I want to present you some convenient code snippets I gathered for [Swift](https://developer.apple.com/swift/).
3+
I want to present some convenient code snippets I gathered for [Swift](https://developer.apple.com/swift/).
44

5-
### All snippets are updated to **Swift 3** ❤️
5+
### All snippets are updated to **Swift 4** ❤️
66

77
## Inspiration
88
I was inspired by Matt Thompson's [Xcode-Snippets](https://github.com/mattt/Xcode-Snippets) article and GitHub repo but I haven't found anything similar for Swift.
@@ -16,15 +16,15 @@ You can simply copy all code snippets (`.codesnippet`) into this folder, restart
1616
Note: Create the folder if it doesn't exist already. It is created by Xcode when user adds his first custom snippet.
1717

1818
## Usage
19-
I named all of snippets using scheme `Swift ...` and set all Completion Shortcuts to `swift-...` so each time you type `swift` in code editor you should see all snippets you can use in that place.
19+
I named all snippets using scheme `Swift ...` and set all Completion Shortcuts to `swift-...` so each time you type `swift` in code editor you should see all snippets you can use in that place.
2020

21-
![Xcode use Swift code snippets](https://raw.githubusercontent.com/burczyk/XcodeSwiftSnippets/master/assets/xcode-use-code-snippet-2.gif)
21+
![Xcode use Swift code snippets](https://raw.githubusercontent.com/burczyk/XcodeSwiftSnippets/master/assets/use.gif)
2222

2323
## How to create your own snippets
2424
To add code snippet to Xcode expand **Utilities** right panel and press `{}` button (*Show the Code Snippet library*) in lower panel.
25-
Then select a code you would like to treat as a snippet, press and hold a left mouse button for a while and drag whole selection to the right panel (if you drag to early you'll probably only deselect text; you can also try dragging from a whitespace instead of the letter).
25+
Then select a code you would like to treat as a snippet, press and hold a left mouse button for a while and drag whole selection to the right panel (if you drag too early you'll probably only deselect text; you can also try dragging from a whitespace instead of the letter).
2626

27-
![Xcode create code snippet](https://raw.githubusercontent.com/burczyk/XcodeSwiftSnippets/master/assets/xcode-add-code-snippet-2.gif)
27+
![Xcode create code snippet](https://raw.githubusercontent.com/burczyk/XcodeSwiftSnippets/master/assets/create.gif)
2828

2929
### Placeholder tokens
3030
If you want to add this nice placeholder in rounded blue rect simply put it between `<#` and `#>` and it will be transformed in both editor and final snippet.
@@ -33,30 +33,33 @@ If you want to add this nice placeholder in rounded blue rect simply put it betw
3333
Currently existing snippets (I hope they will be updated on a regular basis with your help :) ):
3434

3535
```
36-
swift-closureoptionaltypealias Optional closure typealiast with arguments and return value snippet for Swift
37-
swift-closuretypealias Closure typealias with arguments and return value snippet for Swift
38-
swift-prop Auto-Create property stub
39-
swift-didbecomeactive UIApplicationDidBecomeActiveNotification observer snippet for Swift
36+
swift-documentdirectory Document directory path snippet for Swift
4037
swift-dispatchafter GCD dispatch_after snippet for Swift
38+
4139
swift-dispatchasync GCD dispatch_async snippet for Swift
40+
swift-nslocalizedstring NSLocalizedString function snippet for Swift
4241
swift-dispatchmain GCD dispatch_async on main queue snippet for Swift
43-
swift-documentdirectory Document directory path snippet for Swift
42+
swift-closuretypealias Closure typealias with arguments and return value snippet for Swift
43+
swift-weak weakify self in closure
44+
swift-uitableviewdelegate UITableViewDelegate snippet for Swift
4445
swift-forin for-in loop that casts objects inline
45-
swift-iba Create IBAction method stub
4646
swift-ibo Stub out IBOutlet
47-
swift-mail MFMailComposeViewController snippet for Swift
48-
swift-mark Divider label for separating code into sections
49-
swift-message MFMessageComposeViewController snippet for Swift
50-
swift-nslocalizedstring NSLocalizedString function snippet for Swift
51-
swift-singleton Singleton pattern for Swift
5247
swift-sortarrayofstrings Swift String Array locale-aware sorting
53-
swift-uicollectionviewdatasource UICollectionViewDataSource snippet for Swift
48+
swift-prop Auto-Create property stub
49+
swift-closureoptionaltypealias Optional closure typealiast with arguments and return value snippet for Swift
50+
swift-urlsession-delegate Swift HTTP Request With Delegate set
51+
swift-message MFMessageComposeViewController snippet for Swift
52+
swift-uiviewcontrollerlifecycle UIViewController lifecycle snippet for Swift
5453
swift-uicollectionviewdelegate UICollectionViewDelegate snippet for Swift
5554
swift-uiremotenotification UIRemoteNotification registration and handling snippet for Swift
55+
swift-mark Divider label for separating code into sections
56+
swift-didbecomeactive UIApplicationDidBecomeActiveNotification observer snippet for Swift
57+
swift-urlsession-datatask Swift Simple HTTP Request
58+
swift-mail MFMailComposeViewController snippet for Swift
59+
swift-iba Create IBAction method stub
60+
swift-uicollectionviewdatasource UICollectionViewDataSource snippet for Swift
61+
swift-singleton Singleton pattern for Swift
5662
swift-uitableviewdatasource UITableViewDataSource snippet for Swift
57-
swift-uitableviewdelegate UITableViewDelegate snippet for Swift
58-
swift-uiviewcontrollerlifecycle UIViewController lifecycle snippet for Swift
59-
swift-weak weakify self in closure
6063
```
6164

6265

assets/.DS_Store

6 KB
Binary file not shown.
-178 Bytes
Binary file not shown.
-178 Bytes
Binary file not shown.

assets/create.gif

3.68 MB
Loading

assets/create.mov

946 KB
Binary file not shown.

assets/use.gif

5.44 MB
Loading

assets/use.mov

1.33 MB
Binary file not shown.

assets/xcode-add-code-snippet-2.gif

-564 KB
Binary file not shown.

assets/xcode-add-code-snippet.mov

-876 KB
Binary file not shown.

assets/xcode-use-code-snippet-2.gif

-759 KB
Binary file not shown.

assets/xcode-use-code-snippet.mov

-943 KB
Binary file not shown.

swift-closureoptionaltypealias.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<string>ClassImplementation</string>
1010
</array>
1111
<key>IDECodeSnippetContents</key>
12-
<string>typealias &lt;#ClosureName#&gt; = ((_ &lt;#arguments#&gt;) -&gt; &lt;#return value#&gt;)?</string>
12+
<string>typealias &lt;#ClosureName#&gt; = ((_ &lt;#arguments#&gt;: &lt;#type#&gt;) -&gt; &lt;#return value#&gt;)?</string>
1313
<key>IDECodeSnippetIdentifier</key>
1414
<string>1528A53D-69CA-4A12-908A-1F85A47A8644</string>
1515
<key>IDECodeSnippetLanguage</key>

swift-closuretypealias.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<string>ClassImplementation</string>
1010
</array>
1111
<key>IDECodeSnippetContents</key>
12-
<string>typealias &lt;#ClosureName#&gt; = (_ &lt;#arguments#&gt;) -&gt; &lt;#return value#&gt;</string>
12+
<string>typealias &lt;#ClosureName#&gt; = (_ &lt;#arguments#&gt;: &lt;#type#&gt;) -&gt; &lt;#return value#&gt;</string>
1313
<key>IDECodeSnippetIdentifier</key>
1414
<string>42AA2BDB-AA64-4260-9726-A72C20E79FBB</string>
1515
<key>IDECodeSnippetLanguage</key>

swift-ibaction.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
<key>IDECodeSnippetUserSnippet</key>
2424
<true/>
2525
<key>IDECodeSnippetVersion</key>
26-
<integer>0</integer>
26+
<integer>2</integer>
2727
</dict>
2828
</plist>

swift-iboutlet.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<key>IDECodeSnippetUserSnippet</key>
2222
<true/>
2323
<key>IDECodeSnippetVersion</key>
24-
<integer>0</integer>
24+
<integer>2</integer>
2525
</dict>
2626
</plist>

swift-mail.codesnippet

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
func presentModalMailComposerViewController(animated: Bool) {
1515
if MFMailComposeViewController.canSendMail() {
1616
let mailComposeVC = MFMailComposeViewController()
17-
mailComposeVC.delegate = self
17+
mailComposeVC.delegate = &lt;#delegate#&gt;
1818

1919
mailComposeVC.setSubject(&lt;#subject#&gt;)
2020
mailComposeVC.setMessageBody(&lt;#body#&gt;, isHTML: true)
2121
mailComposeVC.setToRecipients([&lt;#recipients#&gt;])
2222

2323
present(mailComposeVC, animated: animated, completion: nil)
2424
} else {
25-
UIAlertView(title: NSLocalizedString("Error", value: "Error", comment: ""), message: NSLocalizedString("Your device doesn't support Mail messaging", value: "Your device doesn't support Mail messaging", comment: ""), delegate: nil, cancelButtonTitle: NSLocalizedString("OK", value: "OK", comment: "")).show()
25+
let alert = UIAlertController(title: NSLocalizedString("Error", value: "Error", comment: ""),
26+
message: NSLocalizedString("Your device doesn't support Mail messaging", value: "Your device doesn't support Mail messaging", comment: ""),
27+
preferredStyle: .alert)
28+
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel"), style: .cancel, handler: nil))
29+
present(alert, animated: true, completion: nil)
2630
}
2731
}
2832

swift-message.codesnippet

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
if MFMessageComposeViewController.canSendText() {
1616
let messageComposeVC = MFMessageComposeViewController()
1717

18-
messageComposeVC.messageComposeDelegate = self
18+
messageComposeVC.messageComposeDelegate = &lt;#delegate#&gt;
1919
messageComposeVC.body = "&lt;#body#&gt;"
2020
messageComposeVC.recipients = [&lt;#recipients#&gt;]
2121

2222
present(messageComposeVC, animated: animated, completion: nil)
2323

2424
} else {
25-
UIAlertView(title: NSLocalizedString("Error", value: "Error", comment: ""), message: NSLocalizedString("Your device doesn't support messaging", value: "Your device doesn't support messaging", comment: ""), delegate: nil, cancelButtonTitle: NSLocalizedString("OK", value: "OK", comment: "")).show()
25+
let alert = UIAlertController(title: NSLocalizedString("Error", value: "Error", comment: ""),
26+
message: NSLocalizedString("Your device doesn't support messaging", value: "Your device doesn't support messaging", comment: ""),
27+
preferredStyle: .alert)
28+
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel"), style: .cancel, handler: nil))
29+
present(alert, animated: true, completion: nil)
2630
}
2731
}
2832

swift-uicollectionviewdatasource.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333

3434
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -&gt; UICollectionReusableView {
35-
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: &lt;#identifier#&gt;, for: indexPath) as UIView
35+
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: &lt;#identifier#&gt;, for: indexPath)
3636
return view
3737
}</string>
3838
<key>IDECodeSnippetIdentifier</key>

swift-uiremotenotification.codesnippet

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@
1818
// let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
1919
// UIApplication.shared.registerUserNotificationSettings(settings)
2020

21-
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
21+
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
2222
print("Successfully egistered for Remote Notifications with token: \(deviceToken)")
2323
}
2424

25-
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
25+
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
2626
print("Registration for Remote Notifications failed with error: \(error)")
2727
}
2828

29-
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
30-
31-
}
32-
33-
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
29+
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
3430

3531
}</string>
3632
<key>IDECodeSnippetIdentifier</key>

swift-weak.codesnippet

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
<key>IDECodeSnippetUserSnippet</key>
2323
<true/>
2424
<key>IDECodeSnippetVersion</key>
25-
<integer>0</integer>
25+
<integer>2</integer>
2626
</dict>
2727
</plist>

0 commit comments

Comments
 (0)