9 Notes In Desember 2019

1. Int to String (Swift 4.2 to Swift 5.1) update 


// Swift 4.2 and old

String(describing: Int()) // OR // "\(Int)"

// Swift 5.1 Update

String(Int()) // OR // "\(Int)" 

Source: https://www.hackingwithswift.com/example-code/language/how-to-convert-an-int-to-a-string

2. Ternary Conditional Operator

// Use If Else

if question {
    answer1
} else {
    answer2
}

// Use Tenary 

hasTrue ? answer1 : answer2
source: https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID71

3. Tabbar Select and UnselectItem color
//use this in viewDidLoad or u can set in Appdelegate

UITabBar.appearance().barTintColor = .mineShaft
UITabBar.appearance().unselectedItemTintColor = .lightGray
UITabBar.appearance().tintColor = UIColor.white

//If U in UITabbarController

tabBar.barTintColor = .black
tabBar.unselectedItemTintColor = .lightGray
tabBar.tintColor = .white

//and if in view contoller use like this (tabbarController?.tabBar...)

4. Get CMTime from QrCode Output 



//MARK: Output func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { captureSession.stopRunning() if let metadataObject = metadataObjects.first { guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return } guard let stringValue = readableObject.stringValue else { return } let cnTimeConvertString = "\(CMTime(value: readableObject.time.value, timescale: 1000000000).positionalTime)"
print(cnTimeConvertString) // "88:51:53" }
} } extension CMTime { var roundedSeconds: TimeInterval { return seconds.rounded() } var hours: Int { return Int(roundedSeconds / 3600) } var minute: Int { return Int(roundedSeconds.truncatingRemainder(dividingBy: 3600) / 60) } var second: Int { return Int(roundedSeconds.truncatingRemainder(dividingBy: 60)) } var positionalTime: String { return hours > 0 ? String(format: "%d:%02d:%02d", hours, minute, second) : String(format: "%02d:%02d", minute, second) } }

result is : print(cnTimeConvertString) // "88:51:53"
source: https://stackoverflow.com/questions/54662047/converting-cmtime-to-string-is-wrong-value-return


5.  UIPopoverPresentationController in uicollectionviewcell swift 4 programmatically
Set popOver in your collectionviewcell, call your UIView file in here:

import UIKit
class PageCollectionViewCell: UICollectionViewCell, UIPopoverPresentationControllerDelegate { static let cellId = "PageCollectionViewCell" @IBOutlet weak var imageView: UIImageView! var didSendSticker: (()-> Void)? override func awakeFromNib() { super.awakeFromNib() // Initialization code let tap = UITapGestureRecognizer(target: self, action: #selector(handleSticker(sender:))) self.isUserInteractionEnabled = true self.addGestureRecognizer(tap) } @objc func handleSticker(sender: UITapGestureRecognizer) { let vc = ReviewStickerViewController() vc.image = imageView.image vc.modalPresentationStyle = .popover vc.preferredContentSize = CGSize(width: 140, height: 180) let popOver = vc.popoverPresentationController popOver?.sourceView = sender.view popOver?.sourceRect = CGRect(x: (sender.view?.bounds.minX)! + (sender.view?.bounds.width)! / 5, y: (sender.view?.bounds.minY)!, width: 50, height: 50) popOver?.permittedArrowDirections = UIPopoverArrowDirection.any vc.popoverPresentationController?.delegate = self vc.didSendSticker = { self.didSendSticker?() } self.window?.rootViewController?.present(vc, animated: true, completion: nil) } func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { return UIModalPresentationStyle.none } }
for present navigation  user ''self.window?.rootViewController?.present", because if use normal navigation  "present " it makes error your code


6. Pod install only one pod

without updating existing repos 

pod install --no-repo-update
This installs new items without updating existing repos (versioned or not).
It's also just quicker if you have a lot of repos and want a fast install of a new item.



source: https://stackoverflow.com/questions/28590013/cocoapods-how-to-install-only-one-new-library

7. Avplayer inside tableview cell still playing when dismissing changing controller


Implement viewWillDisappear to call visibleCells and cycle through them looking for a 
VideoCell. Each time you find one, tell it to stop playing just as in the code you already have.

my answer in stackverflow:  https://stackoverflow.com/a/59188546/8366535
You can use this it's same like viewWillDisappear
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? YouTableViewCell else { return }
    DispatchQueue.main.async {
         cell.videoPlayerContainer.player?.pause()
         cell.videoPlayerContainer.player = nil
         cell.videoPlayerContainer.layoutIfNeeded()
   }
}
Note: this example user AVKit or AVFoundation

8. Remove subString in array Swift 5.1

Example:

let temp = ["29_thumbs.png", "35_thumbs.png", "13_thumbs.png", "24_thumbs.png"]
var newTemp = [String]()


 func tempFilesInto(temp: [String]) {
      temp.forEach { _arr in
          var removeThumb = _arr
          if let range = removeThumb.range(of: "_thumb") {
              removeThumb.removeSubrange(range)
              self.newTemp.append(removeThumb)
          }
      }
  }

print(newTemp) // 29.png, 35.png, 13.png, 24.png

9. Add line break to 'git commit -m' from the command line (Multiple Line)

Try to running in my terminal:

➜  MyApp git:(dev/Barcode) ✗ git add -A
➜  MyApp git:(dev/Barcode) ✗ git commit -m 'CHANGE LOG
quote> fixing adMob
quote> update version
quote> build Update'
[dev/Barcode-For-AppStore 3bb678b]  CHANGE LOG fixing adMob update version build Update
 3 files changed, 8 insertions(+), 12 deletions(-)
➜  MyApp git:(dev/Barcode) git push origin dev/Barcode-For-AppStore
Source: https://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-the-command-line

No comments: