Legacy Constructor

ID

swift.legacy_constructor

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, modernisation

Description

Reports use of legacy C-style geometry / range factory functions in favour of Swift’s named-argument initialisers.

CGRectMake(0, 0, 10, 10)                  // FLAW
CGSizeMake(10, 20)                        // FLAW
NSMakeRange(0, 5)                         // FLAW

CGRect(x: 0, y: 0, width: 10, height: 10) // OK
CGSize(width: 10, height: 20)             // OK
NSRange(location: 0, length: 5)           // OK

Rationale

Named-argument initialisers self-document the call: there’s no positional ambiguity about which number means "width" versus "x". They are also the canonical form in modern iOS / macOS SDK headers.

Remediation

Replace each legacy factory with the corresponding Swift initialiser:

let r = CGRect(x: 0, y: 0, width: 10, height: 10)
let s = CGSize(width: 10, height: 20)
let p = CGPoint(x: 1, y: 2)
let i = UIEdgeInsets(top: 1, left: 2, bottom: 3, right: 4)
let n = NSRange(location: 0, length: 5)