How to access a custom function from any file in the same Swift project?
Any function declared at file scope will have the implicit scope internal
and will be visible in the rest of the project/module.
I recommend reading the access control guide for more information.
Edit:
I'm still not sure what you're trying to do, but it looks like you are mixing global functions with static methods and custom operators.
If what you want is to declare a custom operator that you can use in any other file in the project, the solution is in the documentation that you linked in your question.
So this is what you need to declare a custom % (as you defined it) for the Int type:
CustomOperators.swift
postfix operator %
extension Int {
static postfix func % (n: Int) -> Double {
return Double(n) / 100
}
}
main.swift
print(90%) //outputs "0.9"
That's all there is to it. You simply declare the operator globally: postfix operator %
and you define the operator function as a static method in a extension on the Int type.
Now you can use your new operator in other files (like I did in main.swift
).
Separate out global functions into own file?
Just put them into a separate Swift file. Swift functions (and also classes, structs, enums, etc.) are internal
by default which means that they can be used from anywhere within the same module. As long as the new file belongs to the same module, there's no need to import anything.
You can find more on this topic here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
Can Swift playgrounds see other source files in the same project?
They cannot. Playgrounds are self-contained. This will hopefully change in the future.
Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.
Store a custom object (class in Swift) for access by an Objective-C method
Go to your project’s general settings. Select the proper target for your app. Go to “Build Settings” and switch to “All”, instead of “Basic” which is the default. Here search for the “Packaging” section. Turn on “Defines Module”, by changing “No” to “Yes”.
When this is turned on we will now be able to use swift classes inside
of objective-c files.
Before leaving the “Build Settings” look for “Product Module Name” in the “Packaging” section. This will be important so make sure to take note of and copy the “Product Module Name” exactly.
Next go to an objective-c class where you would like to be able to have access to your swift class. In the implementation or .m file of this class import a header like this:
#import "MyProjectModuleName-Swift.h"
Here the file name of the import must be the exact Project Module Name from the build settings. Any objective-c file where you want access to your swift class should include this import.
- Now it is worth mentioning one potential issue that may arise when using a swift class. Objective-c cannot read top-level swift classes. So if you go to use a method or variable from your swift class directly it will not be recognized. There are one simple solution to this issue. It’s to make your class
public
@objc public class myClass
Member operator '%' must have at least one argument of type 'ViewController’
I declared the ’operator' at file scope
No, you didn't. You defined it in the scope of theUIViewController
definition:
postfix operator %
class ViewController: UIViewController {
// ...
static postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
}
One can define operators as static member functions of a type in Swift 3,
but only if they take at least one argument of that type.
Move the declaration to the file scope to fix the problem:
postfix operator %
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
class ViewController: UIViewController {
// ...
}
Importing pure Swift custom framework into other Swift project
Finally I got the solution with below steps,
Steps
- Mark your custom
MyLog
project asFramework
when creating - Implement
func printLog
and build the project (successful build will create a/Product/MyLog.framework
file) - Copy the
/Product/Mylog.framework
file intoHelloWorld
project directory using Finder - Follow,
HelloWorld Project -> Targets -> Build Phases -> Link Binary With Libraries -> + -> Add Other
(selectMyLog.framework
fromHelloWorld/
directory) - Follow,
HelloWorld Project -> Targets -> Build Phases -> Embed Frameworks -> + -> Other
(selectMyLog.framework
fromHelloWorld/
directory) - Build
HelloWorld
and enjoy!
Update 1
- If you don't find path,
HelloWorld Project -> Targets -> Build Phases -> Embed Frameworks
please check,HelloWorld Project -> Targets -> General -> Embedded Binaries
in the later versions of xcode, which will do the same thing step-5 does.
Where to put reusable functions in IOS Swift?
The best way is to create a helper class with static functions, like this:
class Helper{
static func postRequest() -> [String:String] {
// do a post request and return post data
return ["someData" : "someData"]
}
}
Now every time you need to use postRequest
you can just use like so: Helper.postRequest()
I hope that helps you!
Related Topics
Swiftui View and Uihostingcontroller in Uiscrollview Breaks Scrolling
Swift: Convert String to Hex Color Code
Playing Hls (M3U8) in Cocoa Os X Avplayer - Swift
Swift 3 Type Inference Confusion
Treat a Single Integer Value as a Range in Swift
Core Data with Pre-Filled .Sqlite (Swift3)
Big O of Accessing a String with an Index in Swift 3.0
Swift Ui: Center Text into Circle
Swift Combine: What Are Those Multicast Functions for and How to Use Them
Best Way to Avoid Capturing a Copy of the Value in Closure
How to Respond with an Image Using Vapor
How to Access a Custom Function from Any File in the Same Swift Project
Video Upload to Amazon S3 in Swift
Sktexture Nearest Filtering Mode Doesn't Work (Making Pixel Art)
Type Check Operator (Is) for Check VS Homogenous Protocol: Why Can This Be Done for Optionals
Adding Animation to Tabviews in Swiftui When Switching Between Tabs