Skip to content

Instantly share code, notes, and snippets.

View ikju's full-sized avatar
🏠
Working from home

Ik Ju Song ikju

🏠
Working from home
View GitHub Profile
let input = 10.52834213
// x-> 몇째 자리인지 구하는 변수
x = 10^3 // 10의 3승 -> 1000 만약 2째 자리라면 10의 2승을 넣는다.
let roundedNum = round(input * x) / x
//결과 : 10.528
@ikju
ikju / test
Created January 8, 2020 16:20
//url에 정확한 이미지 url 주소를 넣는다.
let url = URL(string: image.url)
var image : UIImage?
//DispatchQueue를 쓰는 이유 -> 이미지가 클 경우 이미지를 다운로드 받기 까지 잠깐의 멈춤이 생길수 있다. (이유 : 싱글 쓰레드로 작동되기때문에)
//DispatchQueue를 쓰면 멀티 쓰레드로 이미지가 클경우에도 멈춤이 생기지 않는다.
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
DispatchQueue.main.async {
image = UIImage(data: data!)
}
@ikju
ikju / test
Created January 8, 2020 16:26
let t = 8
//1 <= value && value <= 10 처럼 써도 되지만 밑의 코드가 훨씬 더 간결하게 보인다.
if case (1...10) = t {
print("t is between 1 and 10")
}
//case를 쓰기 싫다면 ~을 넣어줘도 좋다.
if (1...10) ~= t {
print("t is between 1 and 10")
let str = " Taylor Swift "
let trimmed = str.trimmingCharacters(in: .whitespacesAndNewlines)
결과 "Taylor Swift”.
//NavigationBar 배경화면 바꾸기
self.navigationController?.navigationBar.barTintColor = .white
//NavgationBar 하단에 회색선 다른색으로 바꾸기
extension UINavigationBar {
func setBottomBorderColor(color: UIColor, height: CGFloat) {
let bottomBorderRect = CGRect(x: 0, y: frame.height, width: frame.width, height: height)
//슬라이드해서 네비게이션 바 위로 올리고 내리기
navigationController?.hidesBarsOnSwipe = true
extension CALayer {
func addBorder(_ arr_edge: [UIRectEdge], color: UIColor, width: CGFloat) {
for edge in arr_edge {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: width)
break
case UIRectEdge.bottom:
border.frame = CGRect.init(x: 0, y: frame.height - width, width: frame.width, height: width)
lazy var stackView: UIStackView = {
//arrangedSubviews <- 안에 있는 값은 클래스에서 불러온값
let stackV = UIStackView(arrangedSubviews: [stackviewF.view1, stackviewF.view2, stackviewF.view3])
stackV.translatesAutoresizingMaskIntoConstraints = false
stackV.axis = .horizontal
stackV.spacing = 10
stackV.distribution = .fillEqually
return stackV
let parameters : Parameters = ["id" : "아이디","password" : "비밀번호"]
// json에서 key값 설정
struct userlist: Codable {
var email : String
var result : String
}
extension UIViewController {
func showToast(message : String, font: UIFont) {
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.font = font
toastLabel.textAlignment = .center;
toastLabel.text = message