Alamofire / AlamofireImage

AlamofireImage is an image component library for Alamofire
MIT License
3.98k stars 523 forks source link

Incorrect associatedObject use generates many Xcode 15 warnings #465

Closed drewster99 closed 1 year ago

drewster99 commented 1 year ago

What did you do?

ℹ Built AlamofireImage using Xcode 15 beta 5.

What did you expect to happen?

ℹ Nothing special.

What happened instead?

ℹ The files UIImage+AlamofireImage.swift, UIImageView+AlamofireImage.swift, and UIButton+Alamofireimage.swift each generate several build warnings of this sort:

Forming 'UnsafeRawPointer' to an inout variable of type String exposes the internal representation rather than the string contents.

Further investigation of this issue shows that, while the warning is new with Xcode 15, the behavior of the code, as it's written, was wrong before that. The warning itself says it all.

Swift Evolution has some discussion on the issue, which recommends this as boilerplate for a better approach:

@propertyWrapper
struct UniqueAddress {
  var _placeholder: Int8 = 0

  var wrappedValue: UnsafeRawPointer {
    mutating get {
      // This is "ok" only as long as the wrapped property appears
      // inside of something with a stable address (a global/static
      // variable or class property) and the pointer is never read or
      // written through, only used for its unique value
      return withUnsafeBytes(of: &self) {
        return $0.baseAddress.unsafelyUnwrapped
      }
    }
  }
}

class Container {
  @UniqueAddress static var key

  func getObject() -> Any? {
    return objc_getAssociatedObject(self, Container.key)
  }
}

There's additional discussion and another alternative in Swift forums.

Alamofire Environment

Alamofire version:4.2.0 Xcode version:15.0beta5 Swift version:5.8.1 Platform(s) running AlamofireImage:iOS macOS version running Xcode:13.4.1

jshier commented 1 year ago

This is fixed in #464. Thanks for the report!

drewster99 commented 1 year ago

This is fixed in #464. Thanks for the report!

Awesome - thanks Jon!