App crashing when trying to share a view - ios

My app is crashing (only for some users) when they click on Share. This is the code:
#IBAction func btnShare(sender: AnyObject) {
let message = "Some message"
let shareUrl = "mydomain://someurl/\(someid)"
if let url = NSURL(string: shareUrl) {
let objectsToShare = [message, url]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]
activityVC.popoverPresentationController?.sourceView = sender as! UIView
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
This is the crash report:
Crashed: com.apple.main-thread
0 libsystem_kernel.dylib 0x184a32014 __pthread_kill + 8
1 libsystem_pthread.dylib 0x184af9460 pthread_kill + 112
2 libsystem_c.dylib 0x1849a63f4 abort + 140
3 libswiftCore.dylib 0x100d83928 swift::fatalError(char const*, ...) + 50
4 libswiftCore.dylib 0x100d6c0f0 swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 70
5 libswiftCore.dylib 0x100d6c180 swift::swift_dynamicCastFailure(swift::Metadata const*, swift::Metadata const*, char const*) + 142
6 libswiftCore.dylib 0x100d92460 swift_dynamicCastObjCClassUnconditional + 68
7 Wize 0x100117198 specialized MyViewController.btnShare(AnyObject) -> () (MyViewController.swift:363)
8 Wize 0x1001132b0 #objc MyViewController.btnShare(AnyObject) -> () (MyViewController.swift)
9 UIKit 0x18b8e27b0 -[UIApplication sendAction:to:from:forEvent:] + 96
10 UIKit 0x18ba565ec -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 168
11 UIKit 0x18b8e27b0 -[UIApplication sendAction:to:from:forEvent:] + 96
12 UIKit 0x18b8e2730 -[UIControl sendAction:to:forEvent:] + 80
13 UIKit 0x18b8ccbe4 -[UIControl _sendActionsForEvents:withEvent:] + 452
14 UIKit 0x18b8ccd4c -[UIControl _sendActionsForEvents:withEvent:] + 812
15 UIKit 0x18b8e201c -[UIControl touchesEnded:withEvent:] + 584
16 UIKit 0x18b8e1b44 -[UIWindow _sendTouchesForEvent:] + 2484
17 UIKit 0x18b8dcd8c -[UIWindow sendEvent:] + 2988
18 UIKit 0x18b8ad858 -[UIApplication sendEvent:] + 340
19 UIKit 0x18c09acb8 __dispatchPreprocessedEventFromEventQueue + 2736
20 UIKit 0x18c094720 __handleEventQueue + 784
21 CoreFoundation 0x185a12278 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
22 CoreFoundation 0x185a11bc0 __CFRunLoopDoSources0 + 524
23 CoreFoundation 0x185a0f7c0 __CFRunLoopRun + 804
24 CoreFoundation 0x18593e048 CFRunLoopRunSpecific + 444
25 GraphicsServices 0x1873c1198 GSEventRunModal + 180
26 UIKit 0x18b918628 -[UIApplication _run] + 684
27 UIKit 0x18b913360 UIApplicationMain + 208
28 Wize 0x1001208f4 main (AppDelegate.swift:19)
29 libdispatch.dylib 0x1849205b8 (Missing)
Any idea what may be going on? It works fine for half the users.

To check the sender before converting it:
guard let sourceView = sender as? UIView else {
return
}
...
activityVC.popoverPresentationController?.sourceView = sourceView

For this below line
activityVC.popoverPresentationController?.sourceView = sender as! UIView
add this line
if let senderSourceView = sender as? UIView {
activityVC.popoverPresentationController?.sourceView = senderSourceView
}else{
//Here not getting UIView
}
It's possible App crash because if UIView not get for sourceView.It is work if right as i said.

Related

Understanding Firebase Crashlytics report for iOS app

I have an iOS app that uses Firebase Crashlytics for reporting crashes. Now I have a report for a crash but my problem is that I could not understand what exactly caused the crash from this report.
Crashlytics report:
Crashed: com.apple.main-thread
0 Nail it 0x104db77a8 CaseDetailsViewController.getbaseData() + 253
(CaseDetailsViewController.swift:253)
1 Nail it 0x104db5168 CaseDetailsViewController.viewDidLoad() + 4335047016 (<compiler-generated>:4335047016)
2 Nail it 0x104db521c #objc CaseDetailsViewController.viewDidLoad() + 4335047196 (<compiler-generated>:4335047196)
3 UIKitCore 0x185d11658 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 100
4 UIKitCore 0x185d160e8 -[UIViewController loadViewIfRequired] + 936
5 UIKitCore 0x185d164f0 -[UIViewController view] + 28
6 UIKit 0x1b913374c -[UIViewControllerAccessibility dismissViewControllerWithTransition:completion:] + 268
7 UIKitCore 0x185d28d6c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 508
8 UIKitCore 0x185d2b474 -[UIViewController dismissViewControllerAnimated:completion:] + 132
9 UIKit 0x1b91333c0 -[UIViewControllerAccessibility dismissViewControllerAnimated:completion:] + 120
10 Nail it 0x104db98ec #objc CaseDetailsViewController.backButton(_:) + 4335065324 (<compiler-generated>:4335065324)
11 UIKitCore 0x18635a72c -[UIApplication sendAction:to:from:forEvent:] + 96
12 UIKitCore 0x185a05474 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 80
13 UIKitCore 0x185a05304 -[_UIButtonBarTargetAction _invoke:forEvent:] + 236
14 UIKitCore 0x18635a72c -[UIApplication sendAction:to:from:forEvent:] + 96
15 UIKitCore 0x185d6aed0 -[UIControl sendAction:to:forEvent:] + 240
16 UIKitCore 0x185d6b228 -[UIControl _sendActionsForEvents:withEvent:] + 396
17 UIKitCore 0x185d6a24c -[UIControl touchesEnded:withEvent:] + 516
18 UIKitCore 0x18639449c -[UIWindow _sendTouchesForEvent:] + 1280
19 UIKitCore 0x186395c64 -[UIWindow sendEvent:] + 3468
20 UIKitCore 0x1863718ec -[UIApplication sendEvent:] + 344
21 UIKit 0x1b90db80c -[UIApplicationAccessibility sendEvent:] + 96
22 UIKitCore 0x1863f2970 __dispatchPreprocessedEventFromEventQueue + 6808
23 UIKitCore 0x1863f54ec __handleEventQueueInternal + 5364
24 UIKitCore 0x1863ed168 __handleHIDEventFetcherDrain + 140
25 CoreFoundation 0x182223ad8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
26 CoreFoundation 0x182223a30 __CFRunLoopDoSource0 + 80
27 CoreFoundation 0x1822231b8 __CFRunLoopDoSources0 + 184
28 CoreFoundation 0x18221e1e8 __CFRunLoopRun + 788
29 CoreFoundation 0x18221dba8 CFRunLoopRunSpecific + 424
30 GraphicsServices 0x18c394344 GSEventRunModal + 160
31 UIKitCore 0x1863593e4 UIApplicationMain + 1932
32 Nail it 0x104d797cc main + 16 (AppDelegate.swift:16)
33 libdyld.dylib 0x1820a58f0 start + 4
Below is the function getBaseData() from CasesDetails View Controller:
func getbaseData(){
let userDefaults = UserDefaults.standard
mobile = userDefaults.value(forKey: "mobile") as! String
altenativeMobile = userDefaults.string(forKey: "altenativeMobile") ?? mobile
privateKey = userDefaults.value(forKey: "privateKey") as! String
////reading JSON
let categoriesJsonString = userDefaults.value(forKey: "categoriesString") as! String
let data = categoriesJsonString.data(using: .utf8)!
if let json = try? JSON(data: data) {
let arrayCats = json["content"][1]["caseMappings"][caseCategoryGlobal].array
if(isNoSubsGlob == 0){
if let arrayCatsCount = arrayCats?.count {
print("The array has been created with \(arrayCatsCount) element")
print("sub1Glob= \(sub1Glob) & sub2Glob= \(sub2Glob)")
for var i in 0..<arrayCatsCount{
let sub1 = json["content"][1]["caseMappings"][caseCategoryGlobal][i]["sub1"].string!
let sub2 = json["content"][1]["caseMappings"][caseCategoryGlobal][i]["sub2"].string ?? ""
if (sub1 == sub1Glob && sub2 == sub2Glob){
departmentId = json["content"][1]["caseMappings"][caseCategoryGlobal][i]["departmentId"].int!
WorkTypeId = json["content"][1]["caseMappings"][caseCategoryGlobal][i]["categoryId"].int!
issueCode = json["content"][1]["caseMappings"][caseCategoryGlobal][i]["id"].int!
}
}
}
}else{
departmentId = json["content"][1]["caseMappings"][caseCategoryGlobal][0]["departmentId"].int!
WorkTypeId = json["content"][1]["caseMappings"][caseCategoryGlobal][0]["categoryId"].int!
issueCode = json["content"][1]["caseMappings"][caseCategoryGlobal][0]["id"].int!
}
}
contactNumberText.text = altenativeMobile
}
and the line 235 is (contactNumberText.text = altenativeMobile) as the last line in the code above
All UI stuff should be performed on main thread, that is what your crash report shows (crash due to main thread.)
So your last line should be :
DispatchQueue.main.async {
self.contactNumberText.text = altenativeMobile
}
Update this line, your crash will fixed.

specialized UIViewController calling some method crashing iOS Swift

I am unable to replicate this issue, how to replicate the following fabric crash ?
Is there a way to open this crash in xcode (Importing crash into xcode)
Crashed: com.apple.main-thread
0 ABCC 0x10092d0c4 specialized ABCViewController.signInBtnPressed(UIButton) -> ()(ABCViewController.swift:178)
1 ABCC 0x10092a520 #objc ABCViewController.unwind(UIStoryboardSegue) -> () + 3152641510
2 UIKit 0x18aced64c -[UIApplication sendAction:to:from:forEvent:] + 86
3 UIKit 0x18ae0e870 -[UIControl sendAction:to:forEvent:] + 52
4 UIKit 0x18acf3700 -[UIControl _sendActionsForEvents:withEvent:] + 120
5 UIKit 0x18ae291a8 -[UIControl touchesEnded:withEvent:] + 492
6 UIKit 0x18b2ceee8 _UIGestureEnvironmentSortAndSendDelayedTouches + 4340
7 UIKit 0x18b2cbc60 _UIGestureEnvironmentUpdate + 1236
8 UIKit 0x18ad664d8 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 404
9 UIKit 0x18ad66010 -[UIGestureEnvironment _updateGesturesForEvent:window:] + 276
10 UIKit 0x18ad65874 -[UIWindow sendEvent:] + 3132
11 UIKit 0x18ad641d0 -[UIApplication sendEvent:] + 340
12 UIKit 0x18b545d1c __dispatchPreprocessedEventFromEventQueue + 2340
13 UIKit 0x18b5482c8 __handleEventQueueInternal + 4744
14 CoreFoundation 0x180f27404 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
15 CoreFoundation 0x180f26c2c __CFRunLoopDoSources0 + 276
16 CoreFoundation 0x180f2479c __CFRunLoopRun + 1204
17 CoreFoundation 0x180e44da8 CFRunLoopRunSpecific + 552
18 GraphicsServices 0x182e2a020 GSEventRunModal + 100
19 UIKit 0x18ae64758 UIApplicationMain + 236
20 ABCC 0x1007b28c4 main (main.m:20)
21 libdyld.dylib 0x1808d5fc0 start + 4
Here is the code for the action that is crashing.
#IBAction func signInBtnPressed(_ sender: UIButton) {
sender.isUserInteractionEnabled = false
guard let employees = UserService.shared.employees, let name = employees.names?[sender.tag] else {
sender.isUserInteractionEnabled = true // crashing here
return
}
// perform task
sender.isUserInteractionEnabled = true
}

Too many crashes about scroll to bottom

I am getting too many crashes from my users for scroll to bottom. What can be cause to this problem?
I am scrolling to bottom my tableview when keyboard appears. And this causes to crash. My code:
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2) {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
}
let numberOfRows = tableView.numberOfRowsInSection(0)
if numberOfRows != 0 {
do {
try tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: numberOfRows-1, inSection: 0), atScrollPosition: .Bottom, animated: false) //This line is crashing
}catch {
print("error")
}
}
}
Crash log:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000010013e018
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [0]
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 Ribony 0x000000010013e018 specialized ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 1904 (ChatViewController.swift:0)
1 Ribony 0x0000000100135890 #objc ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 76 (ChatViewController.swift:0)
2 UIKit 0x000000018e315b58 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 716 (UITableView.m:10665)
3 UIKit 0x000000018e315e00 -[UITableView _createPreparedCellForRowAtIndexPath:willDisplay:] + 88 (UITableView.m:10717)
4 UIKit 0x000000018e31b4a8 -[UITableView _heightForRowAtIndexPath:] + 256 (UITableView.m:12615)
5 UIKit 0x000000018e1127c4 -[UISectionRowData heightForRow:inSection:canGuess:] + 280 (UITableViewRowData.m:288)
6 UIKit 0x000000018e4a413c -[UITableViewRowData ensureHeightsFaultedInForIndexPath:availHeight:edgeInset:scrollPosition:] + 1484 (UITableViewRowData.m:2418)
7 UIKit 0x000000018e1ef4f8 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 1792 (UITableView.m:5544)
8 UIKit 0x000000018e1eecb0 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 52 (UITableView.m:5644)
9 Ribony 0x000000010013ed00 specialized ChatViewController.keyboardWillShow(NSNotification) -> () + 800 (ChatViewController.swift:550)
10 Ribony 0x0000000100136e98 #objc ChatViewController.keyboardWillShow(NSNotification) -> () + 56 (ChatViewController.swift:0)
11 CoreFoundation 0x00000001880b6b10 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20 (CFNotificationCenter.c:650)
12 CoreFoundation 0x00000001880b6214 _CFXRegistrationPost + 400 (CFNotificationCenter.c:164)
13 CoreFoundation 0x00000001880b5f90 ___CFXNotificationPost_block_invoke + 60 (CFNotificationCenter.c:1031)
14 CoreFoundation 0x0000000188125b8c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1504 (CFXNotificationRegistrar.m:163)
15 CoreFoundation 0x0000000187ff7e64 _CFXNotificationPost + 376 (CFNotificationCenter.c:1028)
16 Foundation 0x0000000188b2ce0c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68 (NSNotification.m:482)
17 UIKit 0x000000018e9a6b40 -[UIInputWindowController postStartNotifications:withInfo:] + 400 (UIInputWindowController.m:1375)
18 UIKit 0x000000018e9a8cf0 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.907 + 388 (UIInputWindowController.m:1816)
19 UIKit 0x000000018e0080f0 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 636 (UIView.m:11811)
20 UIKit 0x000000018e0d952c +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 128 (UIView.m:11865)
21 UIKit 0x000000018e9a876c -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 1368 (UIInputWindowController.m:1877)
22 UIKit 0x000000018e9af268 -[UIInputWindowController setInputViewSet:] + 1444 (UIInputWindowController.m:2923)
23 UIKit 0x000000018e9a7e38 -[UIInputWindowController performOperations:withAnimationStyle:] + 56 (UIInputWindowController.m:1674)
24 UIKit 0x000000018e099278 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1276 (UIPeripheralHost.m:4647)
25 UIKit 0x000000018dff8a78 -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] + 80 (UIResponder.m:1107)
26 UIKit 0x000000018e056b4c -[UIResponder becomeFirstResponder] + 600 (UIResponder.m:272)
27 UIKit 0x000000018e056ebc -[UIView(Hierarchy) becomeFirstResponder] + 148 (UIView.m:9419)
28 UIKit 0x000000018e0d90b4 -[UITextField becomeFirstResponder] + 60 (UITextField.m:1396)
29 UIKit 0x000000018e180128 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 192 (UITextInteractionAssistant.m:1160)
30 UIKit 0x000000018e17f630 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3024 (UITextInteractionAssistant.m:1770)
31 UIKit 0x000000018e59af80 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 64 (UIGestureRecognizer.m:103)
32 UIKit 0x000000018e59e688 _UIGestureRecognizerSendTargetActions + 124 (UIGestureRecognizer.m:986)
33 UIKit 0x000000018e16573c _UIGestureRecognizerSendActions + 260 (UIGestureRecognizer.m:1020)
34 UIKit 0x000000018e0040f0 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 764 (UIGestureRecognizer.m:1067)
35 UIKit 0x000000018e58e680 _UIGestureEnvironmentUpdate + 1100 (UIGestureEnvironment.m:148)
36 UIKit 0x000000018e58e1e0 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 408 (UIGestureEnvironment.m:1181)
37 UIKit 0x000000018e58d49c -[UIGestureEnvironment _updateGesturesForEvent:window:] + 268 (UIGestureEnvironment.m:1090)
38 UIKit 0x000000018e00230c -[UIWindow sendEvent:] + 2960 (UIWindow.m:2289)
39 UIKit 0x000000018dfd2da0 -[UIApplication sendEvent:] + 340 (UIApplication.m:10729)
40 UIKit 0x000000018e7bc75c __dispatchPreprocessedEventFromEventQueue + 2736 (UIEventDispatcher.m:1430)
41 UIKit 0x000000018e7b6130 __handleEventQueue + 784 (UIEventDispatcher.m:1626)
42 CoreFoundation 0x00000001880cab5c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1943)
43 CoreFoundation 0x00000001880ca4a4 __CFRunLoopDoSources0 + 524 (CFRunLoop.c:1989)
44 CoreFoundation 0x00000001880c80a4 __CFRunLoopRun + 804 (CFRunLoop.c:2821)
45 CoreFoundation 0x0000000187ff62b8 CFRunLoopRunSpecific + 444 (CFRunLoop.c:3113)
46 GraphicsServices 0x0000000189aaa198 GSEventRunModal + 180 (GSEvent.c:2245)
47 UIKit 0x000000018e03d7fc -[UIApplication _run] + 684 (UIApplication.m:2650)
48 UIKit 0x000000018e038534 UIApplicationMain + 208 (UIApplication.m:4092)
49 Ribony 0x00000001000ce070 main + 120 (AppDelegate.swift:15)
50 libdyld.dylib 0x0000000186fd95b8 start + 4
As you can see i am using this in a try catch block. It should not crash. How is this possible?
Try this code
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2) {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
}
tableViewScrollToBottom(true)
}
func tableViewScrollToBottom(animated: Bool) {
let delay = 0.2 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)
if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
})
}

Swift crash reports don't make sense to me

This is the crash log I got from Crashlytics:
Thread : Crashed: com.apple.main-thread
0 Trenìt! 0x1000b93e4 SearchHistoryProvider.getMostRecentStations(Int) -> [String] (SearchHistoryProvider.swift)
1 Trenìt! 0x10007985c specialized MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift:265)
2 Trenìt! 0x100075414 #objc MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift)
3 UIKit 0x186fa0ad0 -[UIApplication sendAction:to:from:forEvent:] + 100
4 UIKit 0x186fa0a4c -[UIControl sendAction:to:forEvent:] + 80
5 UIKit 0x186f88740 -[UIControl _sendActionsForEvents:withEvent:] + 436
6 UIKit 0x186fa9248 -[UIControl touchesBegan:withEvent:] + 400
7 UIKit 0x186f9fdc0 -[UIWindow _sendTouchesForEvent:] + 376
8 UIKit 0x186f98b08 -[UIWindow sendEvent:] + 784
9 UIKit 0x186f68f4c -[UIApplication sendEvent:] + 248
10 UIKit 0x186f67528 _UIApplicationHandleEventQueue + 6568
11 CoreFoundation 0x181dd5124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
12 CoreFoundation 0x181dd4bb8 __CFRunLoopDoSources0 + 540
13 CoreFoundation 0x181dd28b8 __CFRunLoopRun + 724
14 CoreFoundation 0x181cfcd10 CFRunLoopRunSpecific + 384
15 GraphicsServices 0x1835e4088 GSEventRunModal + 180
16 UIKit 0x186fd1f70 UIApplicationMain + 204
17 Trenìt! 0x10009b2fc main (AppDelegate.swift:14)```
(edited)
and this is my code:
MasterViewController.swift:
263 func showRecentStations(textField: UITextField) {
264 textField.text = ""
265 suggestionStations = masterContainerManager!.homeController?.searchHistoryProvider?.getMostRecentStations(10)
266 updateStationsTable(textField)
267 }
SearchHistoryProvider.swift
func getMostRecentStations (maxSize : Int) -> [String] {
let stationsByOldestArray = getRecentStationsByOldest()
let stations = NSMutableOrderedSet()
for i in (0...(stationsByOldestArray.count-1)).reverse() {
stations.addObject(stationsByOldestArray[i].depStation)
if stations.count==maxSize {
return stations.array as! [String]
}
stations.addObject(stationsByOldestArray[i].arrStation)
if stations.count==maxSize {
return stations.array as! [String]
}
}
return stations.array as! [String]
}
Can anyone understand what's the crash about?
I found the bug:
in case stationsByOldestArray.count was 0
the for loop would have been
for i in (0...-1).reverse()
I fixed it by rewriting the loop as
for i in (0..<stationsByOldestArray.count).reverse()
However I think the Swift crash reports are often hopelessly unhelpful!

Crash in UIActivityViewController in Swift

Im getting an crash in my app when i am using UIActivityViewController here activityItems: [self.myimage.image!] :
let activityViewController = UIActivityViewController(
activityItems: [self.myimage.image!],
applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
myimage is defined as UIImageView :
#IBOutlet weak var myimage: UIImageView!
The stack crash is here :
Thread 0 name:
Thread 0 Crashed:
0 MyPictures 0x000000010008e394 #objc MyPictures.DetailController.ActionShare (My.DetailController)(Swift.AnyObject) -> () + 540 (DetailController.swift:95)
1 UIKit 0x000000018a5f9400 -[UIApplication sendAction:to:from:forEvent:] + 92 (UIApplication.m:3452)
2 UIKit 0x000000018a5f9400 -[UIApplication sendAction:to:from:forEvent:] + 92 (UIApplication.m:3452)
3 UIKit 0x000000018a5e24dc -[UIControl _sendActionsForEvents:withEvent:] + 608 (UIControl.m:647)
4 UIKit 0x000000018a5f8d9c -[UIControl touchesEnded:withEvent:] + 588 (UIControl.m:417)
5 UIKit 0x000000018a5b7fbc _UIGestureRecognizerUpdate + 8532 (UIGestureRecognizer.m:347)
6 CoreFoundation 0x0000000185b3c2a0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28 (CFRunLoop.c:1622)
7 CoreFoundation 0x0000000185b3922c __CFRunLoopDoObservers + 356 (CFRunLoop.c:1718)
8 CoreFoundation 0x0000000185b3960c __CFRunLoopRun + 832 (CFRunLoop.c:2548)
9 CoreFoundation 0x0000000185a652d0 CFRunLoopRunSpecific + 392 (CFRunLoop.c:2795)
10 GraphicsServices 0x000000018f27b6f8 GSEventRunModal + 164 (GSEvent.c:2245)
11 UIKit 0x000000018a62afa8 UIApplicationMain + 1484 (UIApplication.m:3039)
12 MyPictures 0x0000000100091acc main + 152 (AppDelegate.swift:12)
13 libdyld.dylib 0x0000000197a26a04 start + 0 (start_glue.s:78)
It seems like the image is nil that generates a crash, so you should update your code like this:
if let image = self.myimage.image{
let activityViewController = UIActivityViewController(
activityItems: [image],
applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
} else{
// Show some kind of error
}

Resources