Swift 4中如何调用独立函数文件?Matlab用户技术求助
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:
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.swiftis 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]
If you want to reuse the function across multiple projects
- Create a Framework project (File > New > Project > Framework).
- Add
Fibonacci.swiftto the framework, and update the function's access level topublic(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.swiftfile into theSourcesfolder (this folder acts as a mini-module for the Playground). - Update the function to have
publicaccess 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
.swiftfile in Swift—imports are for entire modules. - If the function wasn't marked
public, it had the defaultinternalaccess level, which means it's only visible within its own module.
内容的提问来源于stack exchange,提问作者Harry B

