Post

Replies

Boosts

Views

Activity

Swift Set type performance inconsistently when inserting data
This code will generate different result when RoadmapCardChain  implement Hashable  and Equatable  protocol struct RoadmapCardChain: Codable, Hashable, Equatable { 		let start: String 		let end: String 		let styles: String 		 		static func == (lhs: RoadmapCardChain, rhs: RoadmapCardChain) -> Bool { 				let isStyleSame = lhs.styles == rhs.styles 				let isEndSame = lhs.start == rhs.start && lhs.end == rhs.end 				let isToggleEndSame = lhs.start == rhs.end && lhs.end == rhs.start 				print(lhs) 				print(rhs) 				print("result is \(isStyleSame && ( isEndSame || isToggleEndSame ))") 				return isStyleSame && ( isEndSame || isToggleEndSame ) 		} } let a = RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C") let b = RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C") var chains: Set<RoadmapCardChain> = [] print(a.hashValue) print(b.hashValue) chains.insert(a) if chains.contains(b) { 		print("contains is true") } else { 		chains.insert(b) } print(chains) sometime result is : 785027920194053578 6404817261741129101 RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C") RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C") result is true contains is true [__lldb_expr_19.RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C")] but sometime result is : 491382166321900052 7275567105868021174 [lldb_expr_21.RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C"), lldb_expr_21.RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C")] I don't know why same codes has different performance  I still don't understand why sometimes == be called and sometimes it doesn't be called。According to my understanding Set only need hashvalue when insert or contains function, don't need to call ==
1
0
420
Aug ’20
For a custom Sequence protocol, but index don't change, but use while index change. why?
struct BoxedFibonacciSequence: Sequence, IteratorProtocol { typealias Element = Int var currentIndex = 0 mutating func next() -> Int? { defer { currentIndex += 1 } return loadFibNumber(at: currentIndex) } func makeIterator() -> Self { return self } } func loadFibNumber(at index: Int) -> Int { return fibNumber(at: index) } var box = BoxedFibonacciSequence() for v in box { print("for index",box.currentIndex) if v < 20 { } else { break } } let fib = BoxedFibonacciSequence() var iter = fib.makeIterator() while let v = iter.next() { print("while index",iter.currentIndex) if v < 20 { } else { break } } why use for index always 0, but while index is normal? I'm confused.
0
0
240
Aug ’23
Custom Sequence, use `for` index don't change, but `while` is normal. why?
struct BoxedFibonacciSequence: Sequence, IteratorProtocol { typealias Element = Int var currentIndex = 0 mutating func next() -> Int? { defer { currentIndex += 1 } return loadFibNumber(at: currentIndex) } func makeIterator() -> Self { return self } } func loadFibNumber(at index: Int) -> Int { return fibNumber(at: index) } var box = BoxedFibonacciSequence() for v in box { print("for index",box.currentIndex) if v < 20 { } else { break } } let fib = BoxedFibonacciSequence() var iter = fib.makeIterator() while let v = iter.next() { print("while index",iter.currentIndex) if v < 20 { } else { break } } //out put: //for index 0 //for index 0 //for index 0 //for index 0 //for index 0 //for index 0 //for index 0 //for index 0 //for index 0 //while index 1 //while index 2 //while index 3 //while index 4 //while index 5 //while index 6 //while index 7 //while index 8 //while index 9
1
0
279
Aug ’23
Send UDP Protocol is not working in Xcode 16 iOS18
func setupUDPSocket() { stopSearch() udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main) do { try udpSocket?.bind(toPort: 4012) try udpSocket?.beginReceiving() try udpSocket?.joinMulticastGroup("239.255.255.250") } catch let error { DispatchQueue.main.async { print(Thread.current) print(error) print(error) } } } private func search() { guard let udpSocket = udpSocket else { print("not set udpSocket") stopSearch() return } let message = "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "MX: 3\r\n" + "ST: ssdp:all\r\n" + "\r\n" let data = message.data(using: .utf8)! udpSocket.send(data, toHost: "239.255.255.250", port: 1900, withTimeout: -1, tag: 0) } This is my send SSDP code, my project was inited in Objective-C, recently I update xcode to 16, I get Error Domain=NSPOSIXErrorDomain Code=65 "No route to host", when I send UPD data in iOS 18, but iOS 17 is ok. Even I found, if I init a new project in Swift, this bug is disappear.
1
0
189
3w