r/iOSProgramming • • 4h ago

Discussion Something I never bothered to ask when learning Swift... Why is the standard for Attributes to be inline rather than on a preceding line?

I visually prefer newlines, but code search works marginally better on inlined attributes.

7 Upvotes

10 comments sorted by

6

u/trouthat 4h ago

It’s mostly personal preference. For me if it fits within the maximum number of characters on a line it stays inline otherwise it goes to the next line

3

u/WitchesBravo 4h ago

When attributes were first added they were mostly short and without arguments, just before var/let was easy and worked. Their usage has since grown to encompass more use cases so now newline makes sense

5

u/KukrCZ 4h ago

Strictural visibility is a king/queen. Having var/let starting on the same char is just more structurally dound.

2

u/dawedev 3h ago

Mostly length. Short property wrappers like @State or @Published stay inline because they read as part of the declaration, but anything with arguments like @available(iOS 17, *) usually gets its own line. SwiftLint's opt-in attributes rule lets you pin which ones go where, so the search argument kind of goes away once it's enforced.

2

u/Sea_Wrongdoer_3500 2h ago

i dont think theres a hard standard, its mostly what apple samples do. short property wrappers like u/State private var x read fine on one line so people copied that

swiftlint actually has an attributes rule that kind of codifies the split: same line for vars and imports, own line for funcs and types (@MainActor, u/available etc). thats what i see in most codebases

for long ones like yours tbh preceding line reads better, i wouldnt fight it if the team agrees

•

u/CaffeinatedMiqote 49m ago

Short enough -> inline Too long/annotations have wildly different lengths: everyone goes into a newline because jimmy here is a fat fuck

1

u/MyHairIsATree 2h ago

This is interesting too see, as in Kotlin the equivalent, atl east in terms of syntax - annotations, are usually done using a new line like in the first image

1

u/[deleted] 2h ago

[removed] — view removed comment

1

u/AutoModerator 2h ago

Hey /u/FahadAppMaster, your content has been removed because Reddit has marked your account as having a low Contributor Quality Score. This may result from, but is not limited to, activities such as spamming the same links across multiple subreddits, submitting posts or comments that receive a high number of downvotes, a lack of recent account activity, or having an unverified account.

Please be assured that this action is not a reflection of your participation in our subreddit. This is simply an automated filter in place to reduce spam.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.