직접 만든 DTO를 UserDefaults에 저장 시킬 때 발생하는 오류

 

안드로이드로 비유하자면 implements Serializable같은 과정이 필요함

let encode = JSONEncoder()
let data = try? encode.encode(만든DTO)

 

이 data를 저장하면 오류 해결!

 

참조 : youjean.tistory.com/34

'IOS > SwiftUI' 카테고리의 다른 글

(SwiftUI)16.lazyGridView  (0) 2021.04.29
(SwiftUI)15.피커뷰  (0) 2021.04.28
(SwiftUI)14.토스트,팝업  (0) 2021.04.28
(SwiftUI)13.TextField  (0) 2021.04.28
(SwiftUI)12.커스톰 탭뷰  (0) 2021.04.28
//
//  SegmentLayoutView.swift
//  lazyVGrid_tutorial
//
//  Created by devdepot on 2021/04/29.
//

import SwiftUI


enum LayoutType: CaseIterable {
    case table,grid,multiple
}

extension LayoutType{
    // 레이아웃 타입에 대한 칼럼이 자동으로 설정되도록 한다
    var columns : [GridItem]{
        switch self {
        case .table:
            return[
                // flexible 하나로 한줄로 표현
                GridItem(.flexible())
            ]
        case .grid:
            return [
                // flexible 두개를 넣어서 두개의 레이아웃
                GridItem(.flexible()),
                GridItem(.flexible())
            ]
        case .multiple:
            return [
                //어댑티브를 통해 크기 닿는데 까지 아이템 여러개 쑤셔 넣기
                GridItem(.adaptive(minimum: 100))
            ]
        
    }
}
}

struct SegmentLayoutView: View {
    
    var dummyDataArray = MyModel.dummyDataArray
    
    @State var selectedLayoutType: LayoutType = .table
    
    var body: some View{
        VStack{
            //피커
            Picker(selection: $selectedLayoutType, label: Text("레이아웃 타입"), content: {
                ForEach(LayoutType.allCases,id: \.self,content: {
                    layoutType in
                    switch layoutType{
                    case .table:
                        Image(systemName: "list.dash")
                    case .grid:
                        Image(systemName: "square.grid.2x2.fill")
                    case .multiple:
                        Image(systemName: "circle.grid.3x3.fill")
                    }
                })
            })
            .pickerStyle(SegmentedPickerStyle())
            //내용물
            ScrollView{
                LazyVGrid(columns: selectedLayoutType.columns, content: {
                    ForEach(dummyDataArray){ dateItem in
                   
                        switch selectedLayoutType{
                            case .table:
                                Rectangle().frame(height: 100)
                                    .foregroundColor(.blue)
                        case .grid:
                            RoundedRectangle(cornerRadius: 25.0)
                                .foregroundColor(Color.orange)
                                .frame(height:200)
                                .overlay(
                                    VStack{
                                        Circle()
                                            .frame(height:100)
                                            .foregroundColor(.yellow)
                                        Spacer()
                                            .frame(height:10)
                                        Text("\(dateItem.title)")
                                        Text("\(dateItem.content)")
                                    }
                                )
                        case .multiple:
                            Rectangle().frame(height: 100)
                                .foregroundColor(.green)
                                
                    }
                   
                       
                        
                    }
                   Rectangle()
                })
                .animation(.easeInOut)
            }
        }
    }
}

struct SegmentLayoutView_Previews: PreviewProvider {
    static var previews: some View {
        SegmentLayoutView()
    }
}


'IOS > SwiftUI' 카테고리의 다른 글

(오류)Attempt to set a non-property-list object  (0) 2021.05.12
(SwiftUI)15.피커뷰  (0) 2021.04.28
(SwiftUI)14.토스트,팝업  (0) 2021.04.28
(SwiftUI)13.TextField  (0) 2021.04.28
(SwiftUI)12.커스톰 탭뷰  (0) 2021.04.28
//
//  ContentView.swift
//  picker_swift_tutorial
//
//  Created by devdepot on 2021/04/28.
//

import SwiftUI

enum colorIndex{
    case red,green,blue
}


struct ContentView: View {
    
   
    func getColorByValue(value:Int) -> String{
        switch value {
        case 0:
            return "레드"
        case 1:
            return "그린"
        case 2:
            return "블루"
        default:
            return "레드"
    }
    }
    @State private var selectionValue = 0
//    var colorIdx: colorIndex
    
    
    var body: some View {
        
        VStack(alignment: .center){
            
            Circle()
                .foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
                .frame(width: 50, height: 50)
            Text("세그먼트 value : \(selectionValue)")
            Text("선택된 색깔 : \(self.getColorByValue(value: selectionValue))")
            
            Picker(
                "피커"
                ,selection: $selectionValue
                ,content:{
                    Text("레드").tag(0)
                    Text("그린").tag(1)
                    Text("블루").tag(2)
                }
            ).pickerStyle(SegmentedPickerStyle())
            .cornerRadius(10)
            .padding(.horizontal,10)
            
            Picker(
                "피커"
                ,selection: $selectionValue
                ,content:{
                    Text("레드").tag(0)
                    Text("그린").tag(1)
                    Text("블루").tag(2)
                }
            )
            .frame(width: 70, height: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/)
            .clipped()
            .border(Color.blue, width: 10)
            
           
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

'IOS > SwiftUI' 카테고리의 다른 글

(오류)Attempt to set a non-property-list object  (0) 2021.05.12
(SwiftUI)16.lazyGridView  (0) 2021.04.29
(SwiftUI)14.토스트,팝업  (0) 2021.04.28
(SwiftUI)13.TextField  (0) 2021.04.28
(SwiftUI)12.커스톰 탭뷰  (0) 2021.04.28
//
//  ContentView.swift
//  Toast_popup_tutorial
//
//  Created by devdepot on 2021/04/28.
//

import SwiftUI
import ExytePopupView

struct ContentView: View {
    @State  var shouldShowToast: Bool = false;
    
    
    func creatTopeSolidMessage() -> some View{
        Text("토스트 메세지입니다.")
            .padding()
            .foregroundColor(Color.white)
            .background(Color.blue)
            .cornerRadius(10)
    }
    
    var body: some View {
        
        ZStack{
            VStack{
                Button(action: {
                    self.shouldShowToast = true
                }, label: {
                    Text("토스트 띄우기")
                        .font(.system(size: 25))
                        .foregroundColor(Color.white)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(10)
                })
            }
        }
        .popup(isPresented: $shouldShowToast
               , type: .toast
               , position: .bottom
               , animation: .default
               , autohideIn: 2  //2초가 지난 후 자동으로 사라짐
               , closeOnTap: true
               , closeOnTapOutside: true
               , view: {
                creatTopeSolidMessage()
                
               })
            

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

'IOS > SwiftUI' 카테고리의 다른 글

(SwiftUI)16.lazyGridView  (0) 2021.04.29
(SwiftUI)15.피커뷰  (0) 2021.04.28
(SwiftUI)13.TextField  (0) 2021.04.28
(SwiftUI)12.커스톰 탭뷰  (0) 2021.04.28
(SwiftUI)11.탭뷰  (0) 2021.04.28
//
//  ContentView.swift
//  TextInput_swiftui
//
//  Created by devdepot on 2021/04/28.
//

import SwiftUI

struct ContentView: View {
    
    @State private var username: String = ""
    @State private var password: String = ""
    
    var body: some View {
        
        VStack{
            HStack{
                TextField("사용자 이름",text:$username)
                    .autocapitalization(.none)  //대문자금지
                    .disableAutocorrection(true)    //자동완성 금지
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: {
                    self.username = ""
                }, label: {
                    if(self.username.count > 0 ){
                        Image(systemName: "multiply.circle.fill")
                            .foregroundColor(Color.secondary)
                    }
                  
                })
            }
            HStack{
                SecureField("비밀번호",text:$password)
                    .autocapitalization(.none)  //대문자금지
                    .disableAutocorrection(true)    //자동완성 금지
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: {
                    self.password = ""
                }, label: {
                    Image(systemName: "multiply.circle.fill")
                        .foregroundColor(Color.secondary)
                })
            }
           
           
            Text("입력한 비번 -> \(password)")
        }.padding(.horizontal,50)
        
       
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

'IOS > SwiftUI' 카테고리의 다른 글

(SwiftUI)15.피커뷰  (0) 2021.04.28
(SwiftUI)14.토스트,팝업  (0) 2021.04.28
(SwiftUI)12.커스톰 탭뷰  (0) 2021.04.28
(SwiftUI)11.탭뷰  (0) 2021.04.28
(SwiftUI)10.GeometryReader,enum,애니메이션  (0) 2021.04.28
//
//  bottomViewItem.swift
//  CustomTabView
//
//  Created by devdepot on 2021/04/28.
//

import SwiftUI

enum TabIndex {
    case home
    case cart
    case profile
}
struct MyCustomTabView: View {
    
    @State var tabIndex : TabIndex
    
    @State var largerScale : CGFloat = 1.4
    
    func changeMyView(tabIndex: TabIndex) -> MyView{
        switch tabIndex {
        case .home:
            return MyView(title: "홈", bgColor: Color.green)
        case .cart:
            return MyView(title: "장바구니", bgColor: Color.purple)
        case .profile:
            return MyView(title: "프로필", bgColor: Color.blue)
//        default:
//            <#code#>
        }
    }
    
    func chnageIconColor(tabIndex: TabIndex) -> Color{
       
        switch tabIndex {
        case .home:
            return Color.green
        case .cart:
            return Color.purple
        case .profile:
            return Color.blue
//        default:
//            <#code#>
        }
    }
    
    func calcCircleBgPosition(tabIndex : TabIndex,geometry: GeometryProxy) -> CGFloat{
        switch tabIndex {
        case .home:
            return -(geometry.size.width / 3)
        case .cart:
            return 0
        case .profile:
            return (geometry.size.width / 3)
//        default:
//            <#code#>
        }
    }
    
    var body: some View{
        
        GeometryReader{ geometry in
            ZStack(alignment: .bottom){
                self.changeMyView(tabIndex: tabIndex)
                
                Circle()
                    .frame(width: 90, height: 90)
                    .offset(x: self.calcCircleBgPosition(tabIndex: self.tabIndex, geometry: geometry),y: 12)
                
                HStack(spacing : 0){
                    Button(action: {
//                        print("홈 버튼 클릭")
                        withAnimation{
                            self.tabIndex = .home
                        }
                       
                    }, label: {
                       Image(systemName: "house.fill")
                        .font(.system(size: 25))
                        .scaleEffect(self.tabIndex == .home ? self.largerScale : 1.0)
                        .foregroundColor(self.tabIndex == .home ? self.chnageIconColor(tabIndex: self.tabIndex) : Color.gray)
                        .frame(width:geometry.size.width/3,height: 50)
                    }).background(Color.orange)
                    
                    Button(action: {
//                        print("장바구니 버튼 클릭")
                        withAnimation{
                            self.tabIndex = .cart
                        }
                        
                    }, label: {
                       Image(systemName: "cart")
                        .font(.system(size: 25))
                        .scaleEffect(self.tabIndex == .cart ? self.largerScale : 1.0)
                        .foregroundColor(self.tabIndex == .cart ? self.chnageIconColor(tabIndex: self.tabIndex) : Color.gray)
                        .frame(width:geometry.size.width/3,height: 50)
                    }).background(Color.yellow)
                    
                    Button(action: {
//                        print("프로필 버튼 클릭")
                        withAnimation{
                            self.tabIndex = .profile
                        }
                       
                    }, label: {
                      Image(systemName: "person.circle.fill")
                        .font(.system(size: 25))
                        .scaleEffect(self.tabIndex == .profile ? self.largerScale : 1.0)
                        .foregroundColor(self.tabIndex == .profile ? self.chnageIconColor(tabIndex: self.tabIndex) : Color.gray)
                        .frame(width:geometry.size.width/3,height: 50)
                    }).background(Color.white)
                }
            }
        }.edgesIgnoringSafeArea(.all)
        
       
           
    }
}

struct MyCustomTabView_Previews: PreviewProvider {
    static var previews: some View {
        MyCustomTabView(tabIndex: .home)
    }

}

'IOS > SwiftUI' 카테고리의 다른 글

(SwiftUI)14.토스트,팝업  (0) 2021.04.28
(SwiftUI)13.TextField  (0) 2021.04.28
(SwiftUI)11.탭뷰  (0) 2021.04.28
(SwiftUI)10.GeometryReader,enum,애니메이션  (0) 2021.04.28
(SwiftUI)09.리스트뷰  (0) 2021.04.27

+ Recent posts