ã誰ãæããŠãããªãã£ããã£ãŠã¿ã€ãã«ã§ãããåiOSéçºã«é¢ããŠã¯ç¬åŠã§ãããã£ãŠãªãã®ã§ãããèããã誰ã«ãæãã£ãŠãªãã§ããð§ð§ð§
ãŸããªãããæ¬èªãã§ããããŸãæ瀺çã«ã¯æžããŠãªããã©ããçåœããåã«ãã£ãŠãããïŒããšæã£ãããšãæžããŠãããŸãã
ïŒã©ããã«æžããŠããã®ãèŠèœãšããã ããããããŸãããïŒ
delegateã§èŠæ±ãããŠããprotocolã¯extensionã§æžã
ã«ãŒã«ã§ã¯ãªãã§ãããæ £ç¿çã«extensionã§åprotocolããšã«åé¢ããŠæžããæ¹ããããã§ãã
extension SampleMapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
//do something
}
ããããã£ã®åãOptionalã«ãããšã€ãã·ã£ã«ããªããŠãã
Swiftã®å¶çŽãšããŠãã¯ã©ã¹ã«çŽã¥ãããããã£ã¯å
šéšã€ãã·ã£ã©ã€ã¶ã§åæå€ãäžããããŠããªããšãããŸããã
åºæ¬çã«ã¯åæåããããªãã§ããããããã¿ã€ãã³ã°ã«ãã£ãŠã¯ããã©ããšãããããŸãã
ããããå ŽåãOptionalåãæå®ãããšãã€ãã·ã£ã«ããªããŠãããªããŸãã
class Hoge {
var hoge: String?
init() {
//hogeã¯åæåããªããŠããã
}
}
ãªãã·ã§ãã«ãã§ãŒã³ã¯nilã ãšå®è¡ãããªã
ãããOptionalåã®ç解ã足ããŠãªãã£ã話ãªã®ãããããŸãããã
ãªãã·ã§ãã«ãã§ãŒã³ã䜿ã£ãå Žåãnilã ãšå®è¡ãããŸããã
å
·äœäŸãèŠããšã
var str: String?
print(str?.uppercaseString) // nilïŒuppercaseStringã¯åŠçãããïŒ
str?ã®äžèº«ãnilã§ãªããã°å®è¡ãnilã ã£ããå®è¡ã¹ãããã®ifæãæžããŠããã®ãšåæ§ã®æåãšãªããŸãã
ã€ãŸãnilã®éã¯ããããã£ã¢ã¯ã»ã¹ã»ã¡ãœããã¯å®è¡ãããªãã®ã§ãèæ
®ããªããŠOKã§ãã
frameãæŽæ°ãããšæç»äœçœ®ãæŽæ°ããã
ã³ãŒãã§ViewèŠçŽ ã®äœçœ®ãå€æŽããå ŽåãsetNeedDisplay()ãéœåºŠèŠãã®ããšãæã£ãŠããŸã£ãã®ã§ããã
åºæ¬çã«ã¯frameã®åº§æšæŽæ°ãããã°ãæç»äœçœ®ã¯æŽæ°ãããŸãã
ã³ã³ãã¥ãŒãããããããã£ã¯åŒã¶ãã³ã«åèšç®ãèµ°ã
ãããã誰ãæããŠãããªãã£ãããšããããã¯ç解äžè¶³ç³»ã§ãããã³ã³ãã¥ãŒãããããããã£ã¯åŒã¶ãã³ã«åèšç®ããŸãã
ãããŠèšç®çµæã¯ä¿æãããŸããã
åã¯ãååã«èšç®åŠçãèµ°ããèšç®çµæãåºããïŒåç®ä»¥éã¯ãããäžæžããã ãšåéãããŠããŸããã
ä»ãŸã§ãã£ãèšèªïŒå€ãã®JavaïŒã ãšããããªããããã£ã¯ãªãã£ãã®ã§ããããããå€ãä¿æããŠããªãããšããã®ãç解ã§ããŠããŸããã§ããã
ãã©ãŒã¹ã¢ã³ã©ããããŠããã±ãŒã¹ã¯ïŒã»ãšãã©ïŒãªã
Swift䜿ãå§ãã®é ã¯ããã£ã¡ã
ããã©ãŒã¹ã¢ã³ã©ããã§å€ãåãåºããŠããã®ã§ããã
å匷ãã¯ãããŠããã©ãŒã¹ã¢ã³ã©ããããŠããã±ãŒã¹ã¯ã»ãšãã©ãªãããšã«æ°ã¥ããŸããã
ç¹ã«æ¬çªç°å¢ã§äœ¿ãã³ãŒãã§ããã°ãã³ãŒããåé·ã«ãªã£ãŠããå®å
šã«åãåºããŸãããã
Appleã®å ¬åŒã§ããã©ãŒã¹ã¢ã³ã©ããã䜿ã£ãŠããäŸãïŒåã ãçºèŠããŸããã
// Imported from C
func strlen(_ __s: UnsafePointer<Int8>!) -> UInt
let length = uint8Pointer.withMemoryRebound(to: Int8.self, capacity: 8) {
return strlen($0)
}
// length == 7
ïŒ2020/02/04è¿œèšïŒ
ïŒåç®çºèŠããŸããã
perform(_:)ã§ãã
ãã£ã±é°å²æ°çã«ã¯äœã¬ã€ã€ã®åŠçã®ãšãã«ã ã䜿ã£ãŠããæãã§ãããã
ã¯ããŒãžã£ã¯åºæ¬åŒ±åç §ã§ãããã
URLSession.shared.dataTask(with: url, completionHandler: { [weak self] (data, responce, error) in
let hoge = self?.hoge
}
絶察ã«åŸªç°åç
§ãªããŠããªããïŒããšèšãåããå Žé¢ãªãããã§ããã
æ£çŽãããªå Žé¢ã£ãŠãããŸãªãæ°ãããã®ã§ãã¯ããŒãžã£æžããšãã§äžäœã¹ã³ãŒããžã®ã¢ã¯ã»ã¹ããããšãã¯å
šéšåŒ±åç
§ã§ããããããªãããªãã£ãŠæã£ãŠãŸãã
ïŒéã«åŒ±åç
§ã«ããçµæãå°ãããšã£ãŠã¯ããŒãžã£å
ã®ã³ãŒããå°ãå¢ãããããããããªãã®ã§ïŒ
é åã»èŸæžãåŠçããã¯ããŒãžã£ã¯ç°¡ç¥èšæ³ã掻çšãã
ã©ãããŠã$0ãšã$1ãšãããè¡šèšãæåèŠæã§ããããç¹ã«é«éé¢æ°ïŒmapãšãfilterãšãforEachãšãïŒäœ¿ããããªã±ãŒã¹ã¯ã
é
åã»èŸæžã®èŠçŽ åãåºããšãã¯ã$0ãšãç°¡ç¥èšæ³ã§æžãæ¹ãã¹ã¿ã€ãªãã·ã¥ãªæ°ãããŸãã
ïŒããã¯è°è«ã®äœå°ãããããã§ããïŒ
Setterã ãprivateã«ã§ãã
äžèšã®æžãæ¹ã§ãSetterã ãprivateã«ã§ããŸãã
private(set) var hoge: Bool
ããããã£ã®åæåã§ã¯ããŒãžã£ã䜿ã
æåã³ã³ãã¥ãŒãããããããã£ãšæ··åããŠããŸããŸãããã
ïŒãšããããã®æžãæ¹ã®ããã§ã³ã³ãã¥ãŒãããããããã£ã®æåã誀解ããïŒ
ã¯ããŒãžã£ã䜿ã£ãŠãããããã£ã®åæåãå¯èœã§ãã
let hoge = { return "hoge" }()
ãã®äŸã ãšå®å
šã«ç¡æå³ãªä»£å
¥ã§ãããåæå€å
¥ããéã«ã¡ãã£ãšåæåŠçãå¿
èŠãªãšããªã©ã«äŸ¿å©ã§ãã
ã¯ããŒãžã£ã®çåŸãã«()ãããã®ã§ãã¯ããŒãžã£ã代å
¥ããŠããã®ã§ã¯ãªããã¯ããŒãžã£ã®å®è¡ããåŸã®æ»ãå€ã代å
¥ããŠããŸãã
åæšè«ãç©æ¥µçã«äœ¿ã
ç¹ã«å匷å§ããã°ããã®é ããããªæžãæ¹ãããããŠãŸããã
let hoge: String = "hoge"
代å
¥ããå€ãæããã«Stringåãªã®ã«ããæ瀺çã«æžããæ¹ãããããããã®ããªïŒããšããæèŠãããããå€æ°å: åããæžããŠããŸããã
ãããåºæ¬çã«ã¯ãSwiftã®åæšè«æ©èœã䜿ã£ãŠãçç¥ããã»ããã¹ã¿ã€ãªãã·ã¥ã§ãã
äŸå€ã¯äžèšã¿ãããªã±ãŒã¹ã
let float: Float = 1.0
let cgfloat: CGFloat = 1.0
let double: Double = 1.0
Swiftã¯é¢æ°åã®ææ³ãå ¥ã£ãŠãã
ãªããžã§ã¯ãæåãšãããããã¯é¢æ°åã£ãœãæžãæ¹ãã¢ãã³ãªæããããŸãã
ïŒèŠåºå
žïŒ
enumãç©æ¥µçã«äœ¿ã
䜿ããå Žé¢ã§ããã°ãenumãç©æ¥µçã«äœ¿ããŸãããã
ãŸãšã
çµç€éã«ãªããŸãããã以äžãåã®ã誰ãæããŠããããããšãªã£ãSwiftã®æžãæ¹ã§ããã
äœãã®åœ¹ã«ç«ãŠã°å¹žãã§ãã
誀åè±å/誀ããããŸãããããææ/ç·šéãªã¯ãšã¹ããé¡ãããŸãððð