Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift类的私有访问控制行为是什么?_Swift_Access Modifiers - Fatal编程技术网

swift类的私有访问控制行为是什么?

swift类的私有访问控制行为是什么?,swift,access-modifiers,Swift,Access Modifiers,我用带有Xcode 7 GM种子的故事板尝试了这一点: import UIKit public class C { let _secret = arc4random_uniform(1000) private func secret() -> String { return "\(_secret) is a secret" } } let c1 = C() c1.secret() 这本书给了我“秘密”。因此,这打乱了我对Swift类和对象访问

我用带有Xcode 7 GM种子的故事板尝试了这一点:

import UIKit

public class C {
    let _secret = arc4random_uniform(1000)

    private func secret() -> String {
        return "\(_secret) is a secret"
    }

}
let c1 = C()
c1.secret()

这本书给了我“秘密”。因此,这打乱了我对Swift类和对象访问控制的理解。为什么会发生这种情况?

在Swift
private
中,表示只能在您正在执行的相同源文件中访问。如果您问题中的代码包含在文件
C.swift
中,并且您试图从另一个swift文件访问
secret
方法,则会出现编译时错误


您可以在中阅读有关不同访问修饰符的更多信息。

在Swift
private中
表示只能在您正在执行的相同源文件中访问。如果您问题中的代码包含在文件
C.swift
中,并且您试图从另一个swift文件访问
secret
方法,则会出现编译时错误


有关不同访问修饰符的更多信息,请参阅。

Swift 4更新答案:

有两种不同的访问控制:fileprivateprivate

fileprivate可以从其整个文件中访问

private只能从其单个声明和扩展访问

例如:

// Declaring "A" class that has the two types of "private" and "fileprivate":
class A {
    private var aPrivate: String?
    fileprivate var aFileprivate: String?

    func accessMySelf() {
        // this works fine
        self.aPrivate = ""
        self.aFileprivate = ""
    }
}

// Declaring "B" for checking the abiltiy of accessing "A" class:
class B {
    func accessA() {
        // create an instance of "A" class
        let aObject = A()

        // Error! this is NOT accessable...
        aObject.aPrivate = "I CANNOT set a value for it!"

        // this works fine
        aObject.aFileprivate = "I CAN set a value for it!"
    }
}

有关更多信息,请检查,您也可以检查。

Swift 4更新答案:

有两种不同的访问控制:fileprivateprivate

fileprivate可以从其整个文件中访问

private只能从其单个声明和扩展访问

例如:

// Declaring "A" class that has the two types of "private" and "fileprivate":
class A {
    private var aPrivate: String?
    fileprivate var aFileprivate: String?

    func accessMySelf() {
        // this works fine
        self.aPrivate = ""
        self.aFileprivate = ""
    }
}

// Declaring "B" for checking the abiltiy of accessing "A" class:
class B {
    func accessA() {
        // create an instance of "A" class
        let aObject = A()

        // Error! this is NOT accessable...
        aObject.aPrivate = "I CANNOT set a value for it!"

        // this works fine
        aObject.aFileprivate = "I CAN set a value for it!"
    }
}

有关详细信息,请选中,也可以选中。

私有访问将实体的使用限制为其自己的定义源文件。所有这些都记录在Swift手册的“访问控制”一章中。私有访问将实体的使用限制在其自己定义的源文件中。所有这些都记录在Swift手册的“访问控制”一章中。