Post

Replies

Boosts

Views

Activity

How to create a socket connection and handle data stream in swift ?
Hi, I try to create a socket client to connect to a server, and get/send data as well. My code as below, it connected successfully to the server, but I don't how to receive /send data. Can somebody help me out of this? import Foundation import CoreFoundation class SocketClient {          private(set) var host: String     private(set) var port: Int     private var socket: CFSocket?          init(_ host:String, _ port: Int){         self.host = host         self.port = port     }     func connect(){         //create socket         self.socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, CFSocketCallBackType.dataCallBack.rawValue, {             socket, callBackType, address, data, info in             if CFSocketCallBackType.dataCallBack == callBackType && data != nil {                 debugPrint("incomming。。。")                              }         }, nil)                  guard self.socket != nil else{             debugPrint("create socket failed")             return         }         //binding ip         var address: sockaddr_in = sockaddr_in()         address.sin_family = sa_family_t(PF_INET)         address.sin_port = in_port_t(self.port)         var inAddr = in_addr()         inAddr.s_addr = inet_addr(self.host)         address.sin_addr = inAddr         let addrData = CFDataCreate(kCFAllocatorDefault, &address, MemoryLayout.size(ofValue: address))         let result = CFSocketConnectToAddress(self.socket, addrData, 5)         if result == .success {             debugPrint("connect succeed")         } else {             debugPrint("connect failed")         }         let source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, self.socket, 1)         CFRunLoopAddSource(CFRunLoopGetCurrent(), source, .commonModes)     } }
0
0
300
Oct ’22
how can i get two wheel picker in one row display correctly
How can I get two wheel picker in one row display correctly?I tried to use HStack, but it did not work. see my code below: import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       @State var min:Int = 0   @State var sec:Int = 0       var body: some View {     Form{       Section{         HStack{           Picker("min", selection: $min){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)           Picker("sec", selection: $sec){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)                     }         .frame(height:60)       }     }   } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
1.8k
Oct ’21
DispatchSource compile error: Cannot assign value of type '()' to type 'DispatchSourceTimer?'
After I installed xcode13 and updated pods of my swiftui project by executing pod update, it occurred an unexpected compile error when I tried to run my project. Cannot assign value of type '()' to type 'DispatchSourceTimer?' Is this a new bug? or I did something wrong? See my code below: //use case for counting down time self.timer = DispatchSource.dispatchTimer(timeInterval: 1, handler: { dispatchTimer in       self.time -= 1       if self.time < 0 {         dispatchTimer?.cancel()         self.next()       }     }, needRepeat: true) //DispatchSource extension function public extension DispatchSource {   class func dispatchTimer(timeInterval: Double, handler: @escaping (DispatchSourceTimer?) -> Void, needRepeat: Bool) {           let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)     timer.schedule(deadline: .now(), repeating: timeInterval)     timer.setEventHandler {       DispatchQueue.main.async {         if needRepeat {           handler(timer)         } else {           timer.cancel()           handler(nil)         }       }     }     timer.resume()   } } Please help!
1
0
530
Oct ’21
How to add a search bar to a list?
Hi guys, I try to use .searchable to add search bar to a list, see my code as below, but it doesn't work. How can I make it work? Somebody, please help me!!! import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       var body: some View {     NavigationView {       TabView{         VStack{           List{             Text("aaa")             Text("bbb")             Text("ccc")           }           .searchable(text: .constant(""))         }         .tabItem({           Text("tab1")                     })         VStack{           Text("tab2")         }         .tabItem({           Text("tab2")                     })                 }             }         } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
1.5k
Oct ’21
SwiftUI: How to force a view presented in landscape mode?
I try UIDevice, but occurred error : let value = UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(value, forKey: "orientation") error: 2021-08-23 11:54:05.514193+0800 Spider-IOS[5696:1212060] [error] precondition failure: setting value during update: 805944 dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib AttributeGraph precondition failure: setting value during update: 805944.
0
0
517
Aug ’21
SwiftUI3.0 What the hell is this? Help!
What the hell is this? I code a view in Xcode 13 beta 5 and run it in iOS 15 beta 6. I don't understand what these error message mean, please help! dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib import SwiftUI @available(iOS 15.0, *) struct ExerciseEditView: View {   @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>   @State var headerTitle: String = "Count"   @State var name: String = ""   @State var count: Int = 0   @State var min: Int = 0   @State var sec: Int = 0   @State var weight: Int = 0       var body: some View {     Form{       Section("Name"){         TextField("name", text: $name, prompt: Text("name"))       }                       Section(self.headerTitle){         Picker("pickTime", selection: $headerTitle){           Text("Count")             .tag("Count")           Text("Time")             .tag("Time")         }         .pickerStyle(.segmented)                   if self.headerTitle == "Time" {           Picker("time", selection: $min){             ForEach(0...60, id:\.self){value in               Text("\(value)")                 .tag(value)             }           }           .pickerStyle(.wheel)           .frame(height: 60)         }else {           Picker("count", selection: $count){             ForEach(0...100, id:\.self){value in               Text("\(value)")                 .tag(value)             }           }           .pickerStyle(.wheel)           .frame(height: 60)         }       }       Section("Weight"){         Picker("weight", selection: $weight){           ForEach(0...500, id:\.self){value in             Text("\(value)")               .tag(value)           }         }         .pickerStyle(.wheel)         .frame(height: 60)       }     }     .navigationTitle("Exercise")     .navigationBarTitleDisplayMode(.large)     .navigationBarItems(trailing: HStack{       Button(action:{         presentationMode.wrappedValue.dismiss()       }){         Text("Done")       }     })   } }
0
0
625
Aug ’21
Generic parameter 'Parameters' could not be inferred
hi guys, I try to write a ApiRequest class to handle common request settings and response data, here is a delete function I wrote below: public static func delete<Parameters: Encodable>(_ convertible: URLConvertible, parameters: Parameters? = nil, success: @escaping (Data?)->Void, failure: @escaping (ApiError)->Void) {     debugPrint("ApiRequest.delete->")     debugPrint(convertible)     debugPrint("parameter-> \(parameters)")     AF.request(convertible, method: .delete, parameters: parameters).response{ response in       handleResponse(response, success: success, failure: failure)     }   } I used an optional type-inferred parameter here. I got "Generic parameter 'Parameters' could not be inferred" error when I call this function without parameters: ApiRequest.delete("\(WorkoutApi.routine_delete.rawValue)/\(id)",success: {response in       let index = self.routineList.firstIndex(where: { routine in         routine.id == id       })       if index != nil {         withAnimation(.spring()){           self.routineList.remove(at: index!)         }       }     }, failure: {error in             }) Somebody please help me!! How can I get this code right.Waiting online
1
0
1.4k
Aug ’21