Ios 分段故障11,Xcode 8.2.1,Swift 3

Ios 分段故障11,Xcode 8.2.1,Swift 3,ios,swift,xcode,segmentation-fault,Ios,Swift,Xcode,Segmentation Fault,我正在尝试创建我的应用程序的存档,但在构建iOS设备时遇到了分段错误。在构建模拟器时,我没有遇到这个问题。到目前为止,我已经: 清理了我的项目 清理了我的构建文件夹 已删除我的派生数据文件夹 已安装Mac OS Sierra 更新到Sierra后安装了其他Xcode工具 多次重新启动Xcode/计算机 错误通常如下所示: Call parameter type does not match function signature! 0 swift 0x0

我正在尝试创建我的应用程序的存档,但在构建iOS设备时遇到了分段错误。在构建模拟器时,我没有遇到这个问题。到目前为止,我已经:

  • 清理了我的项目
  • 清理了我的构建文件夹
  • 已删除我的派生数据文件夹
  • 已安装Mac OS Sierra
  • 更新到Sierra后安装了其他Xcode工具
  • 多次重新启动Xcode/计算机
错误通常如下所示:

Call parameter type does not match function signature!
0  swift                    0x000000010f4ab3ad PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000010f4aab56 SignalHandler(int) + 790
2  libsystem_platform.dylib 0x00007fffb1b28bba _sigtramp + 26
3  libsystem_platform.dylib 0x000000011033a000 _sigtramp + 1585517664
4  swift                    0x000000010f3038e8 llvm::TypeFinder::incorporateValue(llvm::Value const*) + 296
5  swift                    0x000000010f3032fa llvm::TypeFinder::run(llvm::Module const&, bool) + 682
6  swift                    0x000000010f1c827e (anonymous namespace)::TypePrinting::incorporateTypes(llvm::Module const&) + 30
7  swift                    0x000000010f1c9bdb printAsOperandImpl(llvm::Value const&, llvm::raw_ostream&, bool, llvm::ModuleSlotTracker&) + 171
8  swift                    0x000000010f30c633 (anonymous namespace)::VerifierSupport::Write(llvm::Value const*) + 67
9  swift                    0x000000010f31616e (anonymous namespace)::Verifier::VerifyCallSite(llvm::CallSite) + 590
10 swift                    0x000000010f318ef3 (anonymous namespace)::Verifier::visitCallInst(llvm::CallInst&) + 35
11 swift                    0x000000010f329ac1 (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 1649
12 swift                    0x000000010f2e089d llvm::FPPassManager::runOnFunction(llvm::Function&) + 973
13 swift                    0x000000010f2e02ab llvm::FPPassManager::runOnModule(llvm::Module&) + 43
14 swift                    0x000000010f2e977a llvm::legacy::PassManager::run(llvm::Module&) + 1514
15 swift                    0x000000010c605901 performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef) + 5921
16 swift                    0x000000010c6038c1 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2625
17 swift                    0x000000010c4b8f31 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23777
18 swift                    0x000000010c4b12b3 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17859
19 swift                    0x000000010c46d5cf main + 8239
20 libdyld.dylib            0x00007fffb191b255 start + 1
/* 
 * This is the offending computed variable.
 */

static var nextImagePath: URL {
  return nextFilePathForDirectoryAtURL(imageDirectory, withExtension: "jpg");
}

/*
 * The method called by above variable. It looks through all the
 * files in a directory, finds the one with the highest index,
 * and returns a new path by incrementing the highest index by 1.
 */

fileprivate static func nextFilePathForDirectoryAtURL(_ url: URL, withExtension ext: String) -> URL {
  guard let files = try? FileManager.default.contentsOfDirectory(
    at: url,
    includingPropertiesForKeys: nil,
    options: .skipsHiddenFiles) else {
      fatalError("Could not create next file path for directory at url: \(url)");
  }

  var maxFileNumber = 0;
  for file in files {
    let fileName = file.deletingPathExtension().lastPathComponent;

    guard
      let fileNumber = Int(fileName),
      file.pathExtension.lowercased() == ext.lowercased()
      else { continue }

    maxFileNumber = max(maxFileNumber, fileNumber);
  }

  return url.appendingPathComponent("\(maxFileNumber + 1).\(ext)");
}

/*
 * Some supporting computed variables for constructing directories.
 */

fileprivate static var libraryDirectory: URL {
  guard let url = try? FileManager.default.url(
    for: .libraryDirectory,
    in: .userDomainMask,
    appropriateFor: nil,
    create: true) else {
      fatalError("Could not create library directory url.");
  }

  return url;
}

fileprivate static var documentSetDirectory: URL {
  let directory = libraryDirectory.appendingPathComponent("MemberDocumentSets");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}

fileprivate static var imageDirectory: URL {
  let directory = documentSetDirectory.appendingPathComponent("Images");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}
我认为这表明编译静态计算变量
nextImagePath
时抛出错误,该变量将
URL
返回到文件路径。在内部,这依赖于一些其他计算变量和方法
nextFilePathForDirectoryAtURL
。总之,代码如下所示:

Call parameter type does not match function signature!
0  swift                    0x000000010f4ab3ad PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000010f4aab56 SignalHandler(int) + 790
2  libsystem_platform.dylib 0x00007fffb1b28bba _sigtramp + 26
3  libsystem_platform.dylib 0x000000011033a000 _sigtramp + 1585517664
4  swift                    0x000000010f3038e8 llvm::TypeFinder::incorporateValue(llvm::Value const*) + 296
5  swift                    0x000000010f3032fa llvm::TypeFinder::run(llvm::Module const&, bool) + 682
6  swift                    0x000000010f1c827e (anonymous namespace)::TypePrinting::incorporateTypes(llvm::Module const&) + 30
7  swift                    0x000000010f1c9bdb printAsOperandImpl(llvm::Value const&, llvm::raw_ostream&, bool, llvm::ModuleSlotTracker&) + 171
8  swift                    0x000000010f30c633 (anonymous namespace)::VerifierSupport::Write(llvm::Value const*) + 67
9  swift                    0x000000010f31616e (anonymous namespace)::Verifier::VerifyCallSite(llvm::CallSite) + 590
10 swift                    0x000000010f318ef3 (anonymous namespace)::Verifier::visitCallInst(llvm::CallInst&) + 35
11 swift                    0x000000010f329ac1 (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 1649
12 swift                    0x000000010f2e089d llvm::FPPassManager::runOnFunction(llvm::Function&) + 973
13 swift                    0x000000010f2e02ab llvm::FPPassManager::runOnModule(llvm::Module&) + 43
14 swift                    0x000000010f2e977a llvm::legacy::PassManager::run(llvm::Module&) + 1514
15 swift                    0x000000010c605901 performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef) + 5921
16 swift                    0x000000010c6038c1 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2625
17 swift                    0x000000010c4b8f31 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23777
18 swift                    0x000000010c4b12b3 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17859
19 swift                    0x000000010c46d5cf main + 8239
20 libdyld.dylib            0x00007fffb191b255 start + 1
/* 
 * This is the offending computed variable.
 */

static var nextImagePath: URL {
  return nextFilePathForDirectoryAtURL(imageDirectory, withExtension: "jpg");
}

/*
 * The method called by above variable. It looks through all the
 * files in a directory, finds the one with the highest index,
 * and returns a new path by incrementing the highest index by 1.
 */

fileprivate static func nextFilePathForDirectoryAtURL(_ url: URL, withExtension ext: String) -> URL {
  guard let files = try? FileManager.default.contentsOfDirectory(
    at: url,
    includingPropertiesForKeys: nil,
    options: .skipsHiddenFiles) else {
      fatalError("Could not create next file path for directory at url: \(url)");
  }

  var maxFileNumber = 0;
  for file in files {
    let fileName = file.deletingPathExtension().lastPathComponent;

    guard
      let fileNumber = Int(fileName),
      file.pathExtension.lowercased() == ext.lowercased()
      else { continue }

    maxFileNumber = max(maxFileNumber, fileNumber);
  }

  return url.appendingPathComponent("\(maxFileNumber + 1).\(ext)");
}

/*
 * Some supporting computed variables for constructing directories.
 */

fileprivate static var libraryDirectory: URL {
  guard let url = try? FileManager.default.url(
    for: .libraryDirectory,
    in: .userDomainMask,
    appropriateFor: nil,
    create: true) else {
      fatalError("Could not create library directory url.");
  }

  return url;
}

fileprivate static var documentSetDirectory: URL {
  let directory = libraryDirectory.appendingPathComponent("MemberDocumentSets");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}

fileprivate static var imageDirectory: URL {
  let directory = documentSetDirectory.appendingPathComponent("Images");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}

我不确定为什么会发生这种错误,或者为什么在构建模拟器时不会发生这种错误。我已经试着寻找答案大约5个小时了,但运气不好,所以我认为这是一篇博文。非常感谢您的帮助。谢谢

Welp,几个小时后,我能够推断错误是由
documentSetDirectory
计算变量引起的。显然,编译器不喜欢将
try
语句的结果转换为可选的。相反,我必须将语句包装在
docatch
块中。以下代码修复了我的问题:

fileprivate static var documentSetDirectory: URL {
  let directory = libraryDirectory.appendingPathComponent("MemberDocumentSets");

  do {
    try FileManager.default.createDirectory(
      at: directory,
      withIntermediateDirectories: true,
      attributes: nil);
  } catch {

    /* 
     * Do nothing. So why have a catch block at all? Because: for some reason
     * this prevents the compiler from spitting up. Apparently it didn't like
     * converting the `try` to an optional here. Weirdly, I'm still doing the
     * optional conversion elsewhere in this same class without issue (see
     * computed variables below).
     *
     * ¯\_(ツ)_/¯
     */
  }

  return directory;
}

fileprivate static var imageDirectory: URL {
  let directory = documentSetDirectory.appendingPathComponent("Images");

  try? FileManager.default.createDirectory(
    at: directory,
    withIntermediateDirectories: true,
    attributes: nil);

  return directory;
}
显然,这一定是编译器的错误。我创建了一个空项目并复制了原始代码,但它编译时没有问题,我无法找到导致相同错误的任何项目设置差异。无论如何,我很高兴找到了一个解决办法,希望它能为未来的一些可怜的人节省一些时间