How can I write a function in a separate swift file and use it (import it) to my ViewController.swift file? I have written a lot of code and all of the code is in the ViewController.swift file, I really need to make this look good and place functions on separate files, for cleaner code. I have functions dealing with parsing HTML, functions dealing with ordering results, presenting results, responding to user actions, etc. Many thanks for any help!
如何在单独的swift文件中编写函数并将其(导入它)用于我的ViewController.swift文件?我编写了很多代码,所有代码都在ViewController.swift文件中,我真的需要让它看起来不错,并将函数放在不同的文件上,以获得更清晰的代码。我有处理解析HTML的函数,处理订购结果的函数,呈现结果,响应用户操作等。非常感谢任何帮助!
if let htmlString = String(contentsOfURL: checkedUrl, encoding: NSUTF8StringEncoding, error: nil) {
// Parsing HTML
let opt = CInt(HTML_PARSE_NOERROR.value | HTML_PARSE_RECOVER.value)
var err : NSError?
var parser = HTMLParser(html: htmlString, encoding: NSUTF8StringEncoding, option: opt, error: &err)
var bodyNode = parser.body
// Create an array of the part of HTML you need
if let inputNodes = bodyNode?.findChildTags("h4") { //inputNodes is an array with all the "h4" tag strings
for node in inputNodes {
let result = html2String(node.rawContents)
println("Nyheter: \(result)")
}
}
When I add that function to a separate swift file, how can I use it in my ViewDidLoad method using a "shorthand"? A short keyword that grabs that chunk of code and use it?
当我将该函数添加到单独的swift文件时,如何使用“速记”在我的ViewDidLoad方法中使用它?一个简短的关键字抓住那块代码并使用它?
3 个解决方案
#1
8
Easy. You just create a new Swift file into your Xcode project (File - New - File - Swift file or just ⌘-N) and put your functions and classes there. No need to import anything in your view controller file as both files are part of the same package and thus see each others functions and types (unless marked as private).
简单。您只需在Xcode项目中创建一个新的Swift文件(文件 - 新建 - 文件 - Swift文件或只是⌘-N)并将您的函数和类放在那里。无需在视图控制器文件中导入任何内容,因为两个文件都是同一个包的一部分,因此可以看到彼此的功能和类型(除非标记为私有)。
func parseHtml(url: NSURL) -> String { ... your code goes here ... }
#2
5
You need to use singletons
.
你需要使用单身人士。
Create NSObject in User.swift
在User.swift中创建NSObject
import Foundation
import UIKit
class User: NSObject {
var name: String = 0
func getName() -> String{
name = "Erik Lydecker"
return name
}
}
Then initialize your object and trigger the method there.
然后初始化您的对象并在那里触发该方法。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let instanceOfUser = User()
instanceOfUser.getName() // Erik Lydecker
}
}
#3
1
You can create a Utils class
, filled with static variables/functions
您可以创建一个包含静态变量/函数的Utils类
For example:
class Utils {
static func convert(to variable: String) -> Int? {
... do some stuff to variable
return newVariable
}
}
// on your ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if let converted = Utils.convert(to: "123") {
print(converted)
}
}
By making use of static functions you can access them anywhere and everywhere to reuse them.
通过使用静态函数,您可以随时随地访问它们以重用它们。
------------------
Another way is to make use of extensions
另一种方法是使用扩展
For example:
extension String {
var toInt: Int? {
return Int(self)
}
}
// on your ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if let converted = "SomeValue".toInt {
print(converted)
}
}
Both of these can be used in many different scenarios.
这两者都可以用于许多不同的场景。
#1
8
Easy. You just create a new Swift file into your Xcode project (File - New - File - Swift file or just ⌘-N) and put your functions and classes there. No need to import anything in your view controller file as both files are part of the same package and thus see each others functions and types (unless marked as private).
简单。您只需在Xcode项目中创建一个新的Swift文件(文件 - 新建 - 文件 - Swift文件或只是⌘-N)并将您的函数和类放在那里。无需在视图控制器文件中导入任何内容,因为两个文件都是同一个包的一部分,因此可以看到彼此的功能和类型(除非标记为私有)。
func parseHtml(url: NSURL) -> String { ... your code goes here ... }
#2
5
You need to use singletons
.
你需要使用单身人士。
Create NSObject in User.swift
在User.swift中创建NSObject
import Foundation
import UIKit
class User: NSObject {
var name: String = 0
func getName() -> String{
name = "Erik Lydecker"
return name
}
}
Then initialize your object and trigger the method there.
然后初始化您的对象并在那里触发该方法。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let instanceOfUser = User()
instanceOfUser.getName() // Erik Lydecker
}
}
#3
1
You can create a Utils class
, filled with static variables/functions
您可以创建一个包含静态变量/函数的Utils类
For example:
class Utils {
static func convert(to variable: String) -> Int? {
... do some stuff to variable
return newVariable
}
}
// on your ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if let converted = Utils.convert(to: "123") {
print(converted)
}
}
By making use of static functions you can access them anywhere and everywhere to reuse them.
通过使用静态函数,您可以随时随地访问它们以重用它们。
------------------
Another way is to make use of extensions
另一种方法是使用扩展
For example:
extension String {
var toInt: Int? {
return Int(self)
}
}
// on your ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if let converted = "SomeValue".toInt {
print(converted)
}
}
Both of these can be used in many different scenarios.
这两者都可以用于许多不同的场景。