swiftã®5ã¤ã®ä¿®é£¾å
Swiftè¨èªã«ããã¦ã修飾åï¼ã¢ã¯ã»ã¹ä¿®é£¾åï¼ã¯ã¯ã©ã¹ãæ§é ä½ãåæä½ãããããã£ãã¡ã½ãããã¤ãã·ã£ã©ã¤ã¶ãå¤æ°ãªã©ã®ã¢ã¯ã»ã¹ã¬ãã«ãå¶å¾¡ããããã«ä½¿ç¨ããã¾ãã以ä¸ã¯ãSwiftã§ä½¿ç¨ã§ãã5ã¤ã®ä¸»è¦ãªä¿®é£¾åã§ãï¼
openï¼æãéãããã¢ã¯ã»ã¹ï¼:
ã©ã®ã¢ã¸ã¥ã¼ã«ããã§ãã¢ã¯ã»ã¹å¯è½ã
ãµãã¯ã©ã¹åããªã¼ãã¼ã©ã¤ããå¯è½ã
public:
ã©ã®ã¢ã¸ã¥ã¼ã«ããã§ãã¢ã¯ã»ã¹å¯è½ã
ãµãã¯ã©ã¹åããªã¼ãã¼ã©ã¤ããå¯è½ã
internal:
åä¸ã¢ã¸ã¥ã¼ã«å
ããã®ã¿ã¢ã¯ã»ã¹å¯è½ã
ã¢ã¸ã¥ã¼ã«å¤ããã¯ã¢ã¯ã»ã¹ä¸å¯ã
fileprivate:
åä¸ãã¡ã¤ã«å
ããã®ã¿ã¢ã¯ã»ã¹å¯è½ã
ãã¡ã¤ã«å¤ããã¯ã¢ã¯ã»ã¹ä¸å¯ã
privateï¼æãå¶ç´ã®ããã¢ã¯ã»ã¹ï¼:
åä¸å®ç¾©ã¹ã³ã¼ãå
ããã®ã¿ã¢ã¯ã»ã¹å¯è½ã
åä¸ãã¡ã¤ã«å
ã§ãå¥ã®åã®æ¡å¼µãå®ç¾©ã§ã¯ã¢ã¯ã»ã¹ä¸å¯ã
ãããã®ä¿®é£¾åã¯ãã³ã¼ãå
ã®ã¨ã³ãã£ãã£ï¼ã¯ã©ã¹ãã¡ã½ãããããããã£ãªã©ï¼ã«å¯¾ãã¦ã¢ã¯ã»ã¹ã®ç¯å²ãå¶å¾¡ãã¾ãã
ä¾ãã°ãç¹å®ã®ã¯ã©ã¹ãä»ã®ã¢ã¸ã¥ã¼ã«ããå©ç¨ã§ããããã«ãããå ´åã¯ãpublic修飾åã使ç¨ãã¾ãã
éã«ãç¹å®ã®ã¡ã½ãããåä¸ã¢ã¸ã¥ã¼ã«å
ã§ã®ã¿å©ç¨å¯è½ã«ããå ´åã¯ãinternal修飾åã使ç¨ãã¾ãã
Swiftã§openãpublicãinternalãfileprivateãprivateã®è¨è¼ä¾
Swiftè¨èªã«ãããã¢ã¯ã»ã¹ä¿®é£¾åã®ä¾ã以ä¸ã«ç¤ºãã¾ãã
以ä¸ã®ä¾ã§ã¯ãããããã®ä¿®é£¾åãã©ã®ããã«ä½¿ç¨ããããã示ãã¦ãã¾ãã
// ã¯ã©ã¹ã®ä¾ open class OpenClass { open var openProperty: Int = 0 public var publicProperty: Int = 0 internal var internalProperty: Int = 0 fileprivate var filePrivateProperty: Int = 0 private var privateProperty: Int = 0 open func openMethod() {} public func publicMethod() {} internal func internalMethod() {} fileprivate func filePrivateMethod() {} private func privateMethod() {} } // åæä½ã®ä¾ public enum PublicEnum { case caseOne case caseTwo } // æ§é ä½ã®ä¾ internal struct InternalStruct { var internalProperty: Int } // ãããã³ã«ã®ä¾ fileprivate protocol FilePrivateProtocol { func filePrivateMethod() } // æ¡å¼µã®ä¾ private extension OpenClass { func privateExtensionMethod() {} } // å¤æ°ã®ä¾ open var openVariable: Int = 0 public var publicVariable: Int = 0 internal var internalVariable: Int = 0 fileprivate var filePrivateVariable: Int = 0 private var privateVariable: Int = 0
ä¸è¨ã®ä¾ã§ã¯ãã¯ã©ã¹ãåæä½ãæ§é ä½ããããã³ã«ãæ¡å¼µãå¤æ°ãªã©ã®æ§ã
ãªã¨ã³ãã£ãã£ã«å¯¾ãã¦ããããã®ä¿®é£¾åãã©ã®ããã«é©ç¨ããããã示ããã¦ãã¾ãã
修飾åã¯ãã¨ã³ãã£ãã£ã®ã¢ã¯ã»ã¹ç¯å²ãå¶å¾¡ããã³ã¼ãã®å¯èªæ§ã¨ä¿å®æ§ãåä¸ãããã®ã«å½¹ç«ã¡ã¾ãã