Skip to content

Commit 2ce4d5b

Browse files
committed
Merge pull request #25 from aryaxt/RemoveForceUnwrapping
get rid of force unwrapping code
2 parents 870ab59 + edbcbc9 commit 2ce4d5b

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

ScrollPager.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Pod::Spec.new do |s|
22
s.name = 'ScrollPager'
3-
s.version = '0.6'
3+
s.version = '0.7'
44
s.summary = 'A fully featured scroll pager similar to the one in flipboard, fully configurable through storyboard'
55
s.homepage = 'https://github.com/aryaxt/ScrollPager'
66
s.license = {
77
:type => 'MIT',
88
:file => 'License.txt'
99
}
1010
s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/ScrollPager'}
11-
s.source = {:git => 'https://github.com/aryaxt/ScrollPager.git', :tag => '0.6'}
11+
s.source = {:git => 'https://github.com/aryaxt/ScrollPager.git', :tag => '0.7'}
1212
s.platform = :ios, '8.0'
1313
s.source_files = 'ScrollPager/Source/*.{swift}'
1414
s.framework = 'Foundation', 'UIKit'

ScrollPager/Source/ScrollPager.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ import UIKit
155155
}
156156

157157
private func addViews(segmentViews: [UIView]) {
158-
for view in scrollView!.subviews {
158+
guard let scrollView = scrollView else { fatalError("trying to add views but the scrollView is nil") }
159+
160+
for view in scrollView.subviews {
159161
view.removeFromSuperview()
160162
}
161163

162164
for i in 0..<segmentViews.count {
163165
let view = segmentViews[i]
164-
scrollView!.addSubview(view)
166+
scrollView.addSubview(view)
165167
views.append(view)
166168
}
167169
}
@@ -196,30 +198,30 @@ import UIKit
196198

197199
UIView.animateWithDuration(animated ? NSTimeInterval(animationDuration) : 0.0, delay: 0.0, options: .CurveEaseOut, animations: { [weak self] in
198200

199-
let width = self!.frame.size.width / CGFloat(self!.buttons.count)
200-
let button = self!.buttons[index]
201+
guard let strongSelf = self else { return }
202+
let width = strongSelf.frame.size.width / CGFloat(strongSelf.buttons.count)
203+
let button = strongSelf.buttons[index]
201204

202-
self?.redrawButtons()
205+
strongSelf.redrawButtons()
203206

204-
if self!.indicatorSizeMatchesTitle {
205-
let string: NSString? = button.titleLabel?.text as NSString?
206-
let size = string?.sizeWithAttributes([NSFontAttributeName: button.titleLabel!.font])
207-
let x = width * CGFloat(index) + ((width - size!.width) / CGFloat(2))
208-
self!.indicatorView.frame = CGRectMake(x, self!.frame.size.height - self!.indicatorHeight, size!.width, self!.indicatorHeight)
207+
if strongSelf.indicatorSizeMatchesTitle {
208+
guard let string = button.titleLabel?.text else { fatalError("missing title on button, title is required for width calculation") }
209+
guard let font = button.titleLabel?.font else { fatalError("missing dont on button, title is required for width calculation") }
210+
let size = string.sizeWithAttributes([NSFontAttributeName: font])
211+
let x = width * CGFloat(index) + ((width - size.width) / CGFloat(2))
212+
strongSelf.indicatorView.frame = CGRectMake(x, strongSelf.frame.size.height - strongSelf.indicatorHeight, size.width, strongSelf.indicatorHeight)
209213
}
210214
else {
211-
self!.indicatorView.frame = CGRectMake(width * CGFloat(index), self!.frame.size.height - self!.indicatorHeight, button.frame.size.width, self!.indicatorHeight)
215+
strongSelf.indicatorView.frame = CGRectMake(width * CGFloat(index), strongSelf.frame.size.height - strongSelf.indicatorHeight, button.frame.size.width, strongSelf.indicatorHeight)
212216
}
213217

214-
if self!.scrollView != nil && moveScrollView {
215-
self!.scrollView?.contentOffset = CGPointMake(CGFloat(index) * self!.scrollView!.frame.size.width, 0)
218+
if let scrollView = strongSelf.scrollView where moveScrollView {
219+
scrollView.contentOffset = CGPointMake(CGFloat(index) * scrollView.frame.size.width, 0)
216220
}
217221

218222
}, completion: { [weak self] finished in
219223
// Storyboard crashes on here for some odd reasons, do a nil check
220-
if self != nil {
221-
self!.animationInProgress = false
222-
}
224+
self?.animationInProgress = false
223225
})
224226
}
225227

@@ -230,11 +232,11 @@ import UIKit
230232
moveToIndex(selectedIndex, animated: false, moveScrollView: false)
231233
}
232234

233-
if scrollView != nil {
234-
scrollView!.contentSize = CGSizeMake(scrollView!.frame.size.width * CGFloat(buttons.count), scrollView!.frame.size.height)
235+
if let scrollView = scrollView {
236+
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(buttons.count), scrollView.frame.size.height)
235237

236238
for i in 0..<views.count {
237-
views[i].frame = CGRectMake(scrollView!.frame.size.width * CGFloat(i), 0, scrollView!.frame.size.width, scrollView!.frame.size.height)
239+
views[i].frame = CGRectMake(scrollView.frame.size.width * CGFloat(i), 0, scrollView.frame.size.width, scrollView.frame.size.height)
238240
}
239241
}
240242
}

0 commit comments

Comments
 (0)