You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 4中如何调用独立函数文件?Matlab用户技术求助

How to Call a Standalone Swift Function From Another File or Playground

Hey there! I totally get where you're coming from—switching from MATLAB's dead-simple standalone .m function setup to Swift's module-based system can feel a bit confusing at first. Let's walk through exactly how to fix this so you can call your fibonacci(until:) function from other files or a Playground without copying code.

First: Understand Swift's Module System

Unlike MATLAB, where any .m file in the same directory is automatically accessible, Swift organizes code into modules. A module is a collection of files that work together, and code within the same module can access each other without imports (as long as access levels allow). You can't directly "import" a single .swift file—you import entire modules instead.


Solution 1: Calling the Function in an Xcode Project

If you're working in a standard Xcode project, follow these steps:

  1. Ensure your function file is in the correct target

    • Open your project settings, go to the Build Phases tab, and check the Compile Sources section.
    • Make sure Fibonacci.swift is listed here. If not, drag the file from your project navigator into this list.
    • Now, any other file in the same target can call fibonacci(until:) directly—no import needed! For example:
      // In another file like ViewController.swift
      let fibSequence = fibonacci(until: 20)
      print(fibSequence) // Output: [0, 1, 1, 2, 3, 5, 8, 13]
      
  2. If you want to reuse the function across multiple projects

    • Create a Framework project (File > New > Project > Framework).
    • Add Fibonacci.swift to the framework, and update the function's access level to public (so it's visible outside the framework):
      public func fibonacci(until limit: Int) -> [Int] {
          var sequence = [0, 1]
          while let last = sequence.last, 
                let secondLast = sequence.dropLast().last, 
                last + secondLast < limit {
              sequence.append(last + secondLast)
          }
          return sequence
      }
      
    • Build the framework, then add it as a dependency to your main project.
    • Import the framework in your main project's files and call the function:
      import MyFibonacciFramework
      let fibSequence = fibonacci(until: 30)
      

Solution 2: Calling the Function in a Playground

Playgrounds work a bit differently—here's how to make your standalone function accessible:

Option A: Project-linked Playground

  • Add the Playground directly to your Xcode project (File > New > Playground, then save it inside your project directory).
  • Ensure the Playground is assigned to the same target as Fibonacci.swift (check the Playground's inspector panel on the right).
  • You can now call fibonacci(until:) directly, no import required.

Option B: Independent Playground

  • Create a new standalone Playground.
  • In the Playground's navigator, right-click the Playground file and select New Folder. Name this folder Sources.
  • Drag your Fibonacci.swift file into the Sources folder (this folder acts as a mini-module for the Playground).
  • Update the function to have public access level (as shown in the framework example above).
  • Import the auto-generated module (named after your Playground) at the top of your Playground, then call the function:
    import MyFibonacciPlayground
    let fibSequence = fibonacci(until: 15)
    print(fibSequence)
    

Why Your Initial Attempt Failed

  • You can't import a single .swift file in Swift—imports are for entire modules.
  • If the function wasn't marked public, it had the default internal access level, which means it's only visible within its own module.

内容的提问来源于stack exchange,提问作者Harry B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:07:36