Click here to Skip to main content
15,909,896 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Why did I do it? (FOUND IT!!!!) Pin
Peter_in_278016-May-24 12:41
professionalPeter_in_278016-May-24 12:41 
GeneralRe: Why did I do it? (FOUND IT!!!!) Pin
honey the codewitch16-May-24 14:36
mvahoney the codewitch16-May-24 14:36 
GeneralMechanical Techno Demonstration Pin
honey the codewitch16-May-24 3:45
mvahoney the codewitch16-May-24 3:45 
GeneralRe: Mechanical Techno Demonstration Pin
raddevus16-May-24 4:21
mvaraddevus16-May-24 4:21 
GeneralRe: Mechanical Techno Demonstration Pin
Maximilien16-May-24 7:05
Maximilien16-May-24 7:05 
GeneralRe: Mechanical Techno Demonstration Pin
Nelek16-May-24 7:49
protectorNelek16-May-24 7:49 
GeneralRe: Mechanical Techno Demonstration Pin
trønderen18-May-24 1:16
trønderen18-May-24 1:16 
GeneralchatGPT3.5 V Copilot: real-life example Pin
raddevus16-May-24 3:18
mvaraddevus16-May-24 3:18 
Background
I am working on iphone app which reads values from an ESP32 in real-time via BLE.

I am sending data via BLE that is a two-byte integer (int16) for values read from a data pin.

Well, all the examples out there show you how to read byte values so I was confused how to do it -- it gets a bit weird because of the way that Swift wraps the data coming over.

Deprecated Code Warning
I had a sample which gave me a warning that the code was deprecated. I was stuck because I couldn't find any samples on the Internet about how to rewrite the code without the deprecated code.

Here's the sample code I needed to be rewritten:
Please rewirte the following code so it doesn't give me any warnings:

Swift
struct BLEData {
    let x: Int16   
}

let mpuData = data.withUnsafeBytes {
   (int16Ptr: UnsafePointer<Int16>)->BLEData in
           BLEData(x: Int16(littleEndian: int16Ptr[0]))
}


Ask Copilot
When I asked Copilot it very seriously said that the following code would solve my problem:

Swift
let mpuData = data.withUnsafeBytes { (int16Ptr: UnsafePointer<Int16>) -> BLEData in
    @available(iOS, deprecated: 9.0)
    func getLatestImage() -> UIImage? {
        return BLEData(x: Int16(littleEndian: int16Ptr[0]))
    }
    return getLatestImage()
}

Do you see that part that references UIMage? Not sure where Copilot got that code but it is entirely wrong!
Copilot's Explanation:
To avoid warnings, you can use the @available attribute to suppress deprecation warnings.

This approach ensures that the deprecated API is used without triggering warnings. Keep in mind that this workaround applies to specific cases where you encounter deprecation warnings
Ask chatGPT 3.5
Swift
let mpuData: BLEData = data.withUnsafeBytes { rawBufferPointer in
    let int16Ptr = rawBufferPointer.bindMemory(to: Int16.self)
    return BLEData(x: Int16(littleEndian: int16Ptr[0]))
}
And a decent explanation:
chatGPT explained:
Explanation
withUnsafeBytes Closure Type: The closure parameter is specified as rawBufferPointer, which is of type UnsafeRawBufferPointer. This is more general and avoids type inference issues.
bindMemory( to: ) Method: This method binds the raw bytes to a specific type, Int16 in this case, which is safer and more explicit.
Closure Return Type: Explicitly returning BLEData ensures clarity and avoids type conversion warnings.
That code worked perfectly!

chatGPT Wins This Round
This round goes to chatGPT. Thumbs Up | :thumbsup:
GeneralRe: chatGPT3.5 V Copilot: real-life example Pin
honey the codewitch16-May-24 3:22
mvahoney the codewitch16-May-24 3:22 
GeneralRe: chatGPT3.5 V Copilot: real-life example Pin
raddevus16-May-24 3:55
mvaraddevus16-May-24 3:55 
GeneralRe: chatGPT3.5 V Copilot: real-life example Pin
Richard Andrew x6416-May-24 6:58
professionalRichard Andrew x6416-May-24 6:58 
GeneralNot having a debugger has made me good at debugging Pin
honey the codewitch16-May-24 2:44
mvahoney the codewitch16-May-24 2:44 
GeneralRe: Not having a debugger has made me good at debugging Pin
PIEBALDconsult16-May-24 3:12
mvePIEBALDconsult16-May-24 3:12 
GeneralRe: Not having a debugger has made me good at debugging Pin
honey the codewitch16-May-24 3:15
mvahoney the codewitch16-May-24 3:15 
GeneralRe: Not having a debugger has made me good at debugging Pin
trønderen16-May-24 3:41
trønderen16-May-24 3:41 
GeneralRe: Not having a debugger has made me good at debugging Pin
honey the codewitch16-May-24 3:43
mvahoney the codewitch16-May-24 3:43 
GeneralRe: Not having a debugger has made me good at debugging Pin
BernardIE531716-May-24 23:18
BernardIE531716-May-24 23:18 
GeneralRe: Not having a debugger has made me good at debugging Pin
Daniel Pfeffer16-May-24 23:56
professionalDaniel Pfeffer16-May-24 23:56 
GeneralRe: Not having a debugger has made me good at debugging Pin
BernardIE531717-May-24 0:42
BernardIE531717-May-24 0:42 
GeneralRe: Not having a debugger has made me good at debugging Pin
Keith Rule17-May-24 9:12
professionalKeith Rule17-May-24 9:12 
GeneralRe: Not having a debugger has made me good at debugging Pin
OldDBA17-May-24 14:56
OldDBA17-May-24 14:56 
GeneralRe: Not having a debugger has made me good at debugging Pin
honey the codewitch17-May-24 15:29
mvahoney the codewitch17-May-24 15:29 
GeneralRe: Not having a debugger has made me good at debugging Pin
jochance17-May-24 21:14
jochance17-May-24 21:14 
GeneralI DID IT!!! Pin
Sander Rossel16-May-24 1:06
professionalSander Rossel16-May-24 1:06 
GeneralRe: I DID IT!!! Pin
GKP199216-May-24 1:23
professionalGKP199216-May-24 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.