4

I was asked to migrate a rather large app to Swift 2. The compiler keeps throwing segmentation fault: 11 errors for one function, present in different modules of the app's logic (only difference being variables used):

func loadMoreContent() {

if let collection = self.ratingsCollection where collection.identifier != 0,
  let totalEntries = collection.totalEntries,
  let objects = self.ratings?.count where objects < totalEntries {

    self.ratingsCollection = nil

    collection.nextPage().onSuccess { (value) in

      if let collection = value as? Collection<Rating> {
        self.ratingsCollection = collection
      } else {
        self.ratingsCollection = Collection<Rating>(identifier: 0)
      }

      }.onFailure { error in
        self.ratingsCollection = Collection<Rating>(identifier: 0)
    }
  }
}

Here are the errors themselves:

1.  While type-checking 'loadMoreContent' at (path redacted).swift:46:3
2.  While type-checking expression at [(path redacted).swift:54:9 - line:64:9] 
    RangeText="collection.nextPage().onSuccess { (value) in

              if let collection = value as? Collection<Rating> {
                self.ratingsCollection = collection
              } else {
                self.ratingsCollection = Collection<Rating>(identifier: 0)
              }

              }.onFailure { error in
                self.ratingsCollection = Collection<Rating>(identifier: 0)
            }"

3.  While loading members for declaration 0x7fdda42ea2b0 at <invalid loc>
4.  While deserializing 'producer' (FuncDecl #340) 

Does anyone have any idea what can be wrong with this function at first glance? I should add it compiles with no changes in Xcode 6 / Swift 1.2.

Laffen
  • 2,753
  • 17
  • 29
michalronin
  • 243
  • 2
  • 12
  • can you isolate which line in loadMoreContent is causing the fault? Maybe start by commenting everything out and adding each line in one by one. My wild guess is that it has something to do with the 'as? Collection' cast. I think it would help to know more about the Collection class you are using. – nwales Dec 16 '15 at 15:39
  • Oddly enough, the error disappeared after I updated ReactiveCocoa, which is not even used either in the Collection class, or the function itself... Pleasures of taking over an abandoned project with no handover, I guess. Many thanks for your input @nwales – michalronin Dec 17 '15 at 14:37

1 Answers1

3

This is a hair pulling error especially common in XCode7.

Occasionally the usual XCode stupid bug protocol (clean, XCode Restart, clean, build) fixes it. However, often it is due to one or more offending lines of code. This doesn't necessarily mean there is a bug in the code, either!

So, before restarting, it is sometimes useful to undo recent changes sequentially and trying to build as you go along. If any of your dependencies or frameworks have been updated since your last successful build, these could be a likely candidate.

There are a couple things that seem to produce this error fairly regularly. So please add to this list concisely if you can isolate specific issues that CONSISTENTLY cause errors for you:

1) String concatenation using the plus operator in calls to methods that use autoclosures (found in calls to XCGLogger):

 public func myFunc(@autoclosure closure: () -> String?){
      // do something
 }

 someInstance.myFunc("Hi " + nameStr + "!")

2) failure to call super.init() from subclass especially when super class is using a default initializer (you haven't explicitly created your own init)

3) Accidentally using a single equals sign to test for equality (using = instead of == ) especially in complex statement such as in this answer.

Community
  • 1
  • 1
WaterNotWords
  • 997
  • 1
  • 9
  • 24