ã¯ããã«
ãSwiftã¯2014å¹´ã®å ¬é以æ¥ã3.1ã¾ã§ãã¼ã¸ã§ã³ãæ´æ°ããã¦ãã¾ããããã¼ã¸ã§ã³ãä¸ããã¨åæã«å¤§ããªä»æ§ãç¸æ¬¡ããäºææ§ããªãã£ããã次ã®ãã¼ã¸ã§ã³ã¢ããã¸ã®ä¸å®ãããã¾ããã
ãã§ããSwift 3ãã4ã¸ã®ãã¼ã¸ã§ã³ã¢ããã§ãã¡ã½ãããé¢æ°ã®å®ç¾©ãå½åè¦åãçµ±ä¸ãããä»å¾ã¯æ¥µç«¯ãªä»æ§ã®å¤æ´ãªã©ããªããªãã¨èãããã¦ãã¾ãã
ãé£è¼ã®1åç®ã§ã¯ãSwift 3以éã§å¤æ´ãããã¡ã½ãããé¢æ°ãä¸å¿ã«ãã¢ããªéçºãè¡ãä¸ã§ã®åºæ¬çãªãã¤ã³ããã¬ãã¥ã¼çã«èª¬æãã¾ããåé ç®ã§èª¬æããå 容ã«ã¤ãã¦ã®Swiftã®ãã¼ã¸ã§ã³ã¯è¦åºãã®ããå ã«è¨ãã¾ãã
ãæ¬é£è¼ã¯åé 触ããéããã¢ããªéçºã®æåã«ç¥ã£ã¦ããã¹ãåºæ¬çãªäºæã®èª¬æãã¡ã¤ã³ãã¼ãã¨ãã¦ãã¾ããå 容ã«ãã£ã¦ã¯ãSwift 4以åã®ãã¼ã¸ã§ã³ã«è§¦ãããã¨ãSwiftèªä½ã®è¨èªä»æ§ãªã©ã«ã¯èª¬æãåã°ãªããã¨ããäºæ¿ãã ãããæ¬é£è¼ä»¥ä¸ã®å種æ å ±ã¯æ«å°¾ã®åèæç®çãåç §ãã¦ãã ããã
ãé£è¼ç¬¬1åã§ã¯æ¬¡ã®å 容ã«ã¤ãã¦èª¬æãã¾ãã
- Swift 3以éã§å¤æ´ãããã¡ã½ãããé¢æ°ã®åºæ¬çãªå½åè¦åãå®ç¾©ã®ä»æ¹
- ã¯ã©ã¹ã®æ¡å¼µæ¹æ³ã¨ã½ã¼ã¹ã³ã¼ãã®åå²
対象èªè
ãæ¬è¨äºã¯ã次ã®æ¹ã対象ã«ãã¦ãã¾ãã
- Swiftã§ã®åºæ¬çãªããã°ã©ã ãã§ããæ¹
- Xcodeã使ããæ¹
Xcodeã®ã¤ã³ã¹ãã¼ã«ã¨Swiftã®ãã¼ã¸ã§ã³ã®ç¢ºèª
ãSwift 4ã使ã£ãã¢ããªéçºãå§ããåã«ãå®è¡ç°å¢ã®ç¢ºèªããã¦ããã¾ãã
Xcodeã®ã¤ã³ã¹ãã¼ã«
ãXcodeã¯ãAppleã®éçºè åããµã¤ããµã¤ãã§ããã¾ã§ã«å ¬éããããã¼ã¸ã§ã³ã¨ãããããæ£å¼çã«ãªãäºå®ã®ãã¼ã¿çãå«ããè¤æ°ã®ãã¼ã¸ã§ã³ãé å¸ããã¦ãã¾ããæ¬é£è¼å·çæã®æç¹ã§ã¯ãXcode 9ãæ£å¼çã®ææ°ãã¼ã¸ã§ã³ã§ããApp Storeã§é å¸ããã¦ããXcodeãæ£å¼çã®ææ°ãã¼ã¸ã§ã³ãªã®ã§ãApp Storeãçµç±ãã¦ã¤ã³ã¹ãã¼ã«ãã¦ããã¦ãã ããã
Swiftã®ãã¼ã¸ã§ã³ç¢ºèª
ãXcodeã®ã¤ã³ã¹ãã¼ã«ã¨ã¨ãã«ãSwiftã®å®è¡ç°å¢ãã¤ã³ã¹ãã¼ã«ããã¾ããã¤ã³ã¹ãã¼ã«ãããSwiftã®ãã¼ã¸ã§ã³ã確èªããããã«ã¯ãã¿ã¼ããã«ãã次ã®ã³ãã³ããå®è¡ãã¾ãã
$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -version
ããApple Swift version 4.0ãã®ããã«ãSwiftã®ãã¼ã¸ã§ã³ã表示ããããã¨ã確èªãã¦ãã ããã
ã¡ã½ãããé¢æ°ã®å®ç¾©ãSwift 3ã
ãSwiftã®ãã¼ã¸ã§ã³2ã¨ãã¼ã¸ã§ã³3ã§ã¯ã¡ã½ãããé¢æ°ã®å®ç¾©ã®æ¹æ³ãç°ãªãã¾ãããSwift 4ã§ã¯ãSwift 3以éã®æ¹å¼ãæ¡ç¨ãããã®ã§ããã®æ¹éã«æ²¿ã£ã¦å®ç¾©ãããã¡ã½ãããé¢æ°ã«ã¤ãã¦ãä¸å³ã®ãããªãã¼ãã«ãã2ã¤ã®ç»é¢ã表示ãããµã³ãã«ã使ããå ·ä½çã«èª¬æãã¾ãã
ã¡ã½ããã®å®ç¾©
ãSwift 3以åã¨æ¯è¼ããã¨ãåãã¡ã½ããã§ãã¡ã½ããåèªä½ãçããªãã¾ãããViewControllerãã¢ã¼ãã«ã§è¡¨ç¤ºããä¾ã¨ãå°å³ä¸ã®ãã³ãã¿ãããã¦å¹ãåºãã表示ããä¾ã¯ä»¥ä¸ã®éãã§ãã
// Main.storyboardãåå¾ let storyBoard = UIStoryboard(name: "Main", bundle: nil) let row = indexPath.row if row == 0 { // CatNavigationViewControllerã®IDã®ViewControllerãåå¾ãã¦ã¢ã¼ãã«è¡¨ç¤º let vc = storyBoard.instantiateViewController(withIdentifier: "CatNavigationViewController") self.present(vc, animated: true, completion: nil) // --------ï¼1ï¼ }
// ãã³ãã¿ããããéã«å¹ãåºãã表示ãã func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { // --------ï¼2ï¼ // å¹ãåºããåå¾ var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: self.identifier) //èµ·ãåºããªãã¸ã§ã¯ãã空ã®å ´åã¯çæ if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView!.canShowCallout = true } else { annotationView!.annotation = annotation } return annotationView }
ãSwift 3以éã§ã¯ãã¢ã¼ãã«è¡¨ç¤ºãè¡ãã¡ã½ããï¼1ï¼ãå¹ãåºãã表示ããã¡ã½ããï¼2ï¼ã¯ãã®ããã«è¨è¿°ãã¾ãããã®2ã¤ã®ã¡ã½ãããSwift 2以åã¨æ¯è¼ãããã®ã次ã®è¡¨ã§ãã
ç¨®é¡ | Swift 2以å | Swift 3以é |
---|---|---|
ã¢ã¼ãã«è¡¨ç¤ºãè¡ãã¡ã½ãã | presentViewController(_:animated:completion:) | present(_:animated:completion:) |
å¹ãåºãã表示ããã¡ã½ãã | mapView(mapView:viewForAnnotation:) | mapView(_:viewFor:) |
ãSwift 2以åã¨æ¯ã¹ãã¨ãSwift 3以éã§ã¯ã¡ã½ããåã¨å¼æ°ã®ã©ãã«ãçç¥ããããçããªã£ãããã¦ãããã¨ããããã¾ãããããã®äºæã¯ä»ã®ã¡ã½ããã§ãå ±éãã¦ããããã¨ã§ãã
ããã®ã¡ã½ããã®å¤æ´ç¹ãã¾ã¨ããã¨æ¬¡ã®ããã«ãªãã¾ãã
- ã¡ã½ããåèªä½ãçãç°¡æ½ã«ãªã£ã
- å¼æ°ã®ã©ãã«ãã¡ã½ããã¨åæ§ã«ç°¡ç¥åããã
- 第ä¸å¼æ°ã®ã¡ã½ããã¯çç¥å¯è½ãªå ´åãå¤ããªã£ã
ãSwift 2以åã¨æ¯è¼ããã¨ãã¡ã½ããé¢é£ã¯é常ã«ç°¡æ½ã«æ¸ããããã«ãªãã¾ãããå ·ä½çã«ã¯ç¬¬ä¸å¼æ°ã®ã©ãã«ã¯çç¥ããããã¨ãå¤ããªãã¾ãããä¾ãã°ãInterfaceBuilderãå©ç¨ãã¦éãããã¿ã³ãæ¼ããéã®å¦çãè¨è¿°ããå ´åã«ããã¡ã½ããåãXcodeã®æ©è½ã§è¨è¿°ãããæç¹ã§ç¬¬ä¸å¼æ°ã®ã©ãã«ã®åã«ã_ãã表示ãããã¡ã½ããã®å®è¡æã«ç¬¬ä¸å¼æ°ã®ã©ãã«ã¯çç¥ããã¾ãã
ãéçºè èªèº«ãã¡ã½ãããä½æããå ´åã«ãããã®ã«ã¼ã«ã«å¾ã£ã¦ç¬¬ä¸å¼æ°ã®ã©ãã«ã¯çç¥ãã¦ä½æãããã¨ããå§ããã¾ãã
ãã¡ã½ããåãçããªã£ãããããã¤ã¦ã®ãxxxWithï½ãç³»ã®ã¡ã½ãããªã©ã¯ãååãxxxã«çµ±ä¸ããã¾ããããã®å ´åã«ã¯ç¬¬ä¸å¼æ°ã®ã©ãã«ãåå¨ãããã®ã©ãã«åã§ã¡ã½ããã®å½¹ç®ãåºå¥ãã¾ãããµã³ãã«å ã®å ·ä½çãªä¾ã¯æ¬¡ã®éãã§ãã
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // UITableViewã«ç»é²ããã¦ã»ã«ãåå¾ let cell = self.tableView.dequeueReusableCell(withIdentifier: self.Identifier) // --------ï¼3ï¼ cell?.textLabel?.text = items[indexPath.row] return cell! }
ããã¼ãã«ã«ç»é²ãããã»ã«ãåå¾ããã¡ã½ããã¯ãï¼3ï¼ã®ããã«ã¡ã½ããåãçããªãã()ã®ä¸ã«ç¬¬ä¸å¼æ°ã®ã©ãã«ã¨ãã¦withIdentifierãå ¥ãã¾ããåãã¡ã½ããã§ããSwift 2以åã§ã¯ãªã¹ã5ã®éãã«è¨è¿°ãã¾ãã
let cell = self.tableView.dequeueReusableCellWithIdentifier(self.Identifier)
ããã®ããã«Swift 3以éã§ã¯ãSwift 2以åã®ã¡ã½ããåã®ãWithãã®é¨åããååã¨å¼æ°ãåããããã«ãªãã¾ããã
é¢æ°ã®å®ç¾©
ãã¡ã½ããåã¨åæ§ã«é¢æ°åãçããªãã¾ããããxxxMakeãçã®é¢æ°ã¯Makeãçç¥ããã¦çãååã«ãªãã¾ããã
// UIImageViewã®ç©å½¢ãæå® imageView.frame = CGRect(x:0, y:0, width:200, height:130) // --------ï¼1ï¼ // UIImageViewã®ä¸å¿ã®ä½ç½®ãæå® imageView.center = CGPoint(x:self.view.center.x, y:self.view.center.y) // --------ï¼2ï¼ imageView.contentMode = .scaleAspectFit self.view.addSubview(imageView)
ãCGRecté¢æ°ã§ç©å½¢ãCGPointé¢æ°ã§ä½ç½®ã®ãªãã¸ã§ã¯ããçæã§ãã¾ããSwift 2以åã§ã¤ãããã¦ãããé¢æ°åã®å¾ãã®ãMakeãã¯ä¸è¦ã§ãã
ãã¡ã½ããã®å ´åã¨éã£ã¦ãé¢æ°ã®å ´åã¯ååãçããªã£ã¦ã第ä¸å¼æ°ã¯çç¥ããã¦ããªãç¹ã«ã注æãã ããã