Harish Harish - 1 month ago 18
iOS Question

Command Failed due to signal: Segmentation Fault 11 -- Xcode 8 Swift 3

After some intense googling and brainstorming, I can't find a solution so let's see if Stack Overflow has the magic solution I need. I am using the Contacts framework to fetch dates from a contact. However, this works well but I need to sort the dates in order for this to work. As a result, I am

extending
an
Array
of
CNContact
, and inside the extension I have a function to do my work. No errors show up in the
Issue Navigator
but when I build I get an issue in the
Report Navigator
saying
Command Failed due to signal: Segmentation Fault 11
. I really believe that the issue is with my use of Generics because the report navigator is pointing to that line, and my googling suggests that that is where the issue is coming from but I have no workarounds. (I highly suspect this but I'm not completely sure.)

Let me share my code with you:
This is inside my extension of the array of CNContacts.

typealias ContactDate = CNLabeledValue<NSDateComponents>
func filteredAndSortedDates() -> [ContactDate : CNContact] { // Error occurs on this line according to the issue.

var allDates: [ContactDate : CNContact] = [:]

var sortedDates: [ContactDate: CNContact] = [:]


for contact in self {
if contact.isKeyAvailable(CNContactDatesKey) {
for date in contact.dates {
allDates[date] = contact
}
}
}

for (key, value) in (Array(allDates).sorted {
date1, date2 in
if date1.key.value.month == date2.key.value.month {
return date1.key.value.day < date2.key.value.day
} else {
return date1.key.value.month < date2.key.value.month
}
}) {
sortedDates[key] = value
}

return sortedDates
}


Here are screenshots of the issue:
error1
error2

Answer

Your code is causing the Swift compiler to crash when you assign an empty dictionary to allDates and/or sortedDates:

var allDates: [ContactDate: CNContact] = [:]
var sortedDates: [ContactDate: CNContact] = [:]

At first I thought it was due to CNLabeledValue not conforming to Hashable but I don't think it's a problem with your code. For example this crashes the compiler:

var crashingDict: [CNLabeledValue<NSDateComponents>: CNContact] = [:]

But this doesn't:

 var workingDict: [CNContact: CNLabeledValue<NSDateComponents>] = [:]

And in second example, CNContact as a key doesn't conform to Hashable either.

My advice would be to swap the values in the dict and get your code working that way. It would be best to file a bug report with Apple.