"Attribute can only be applied to types, not declarations" error

I am gettting this error

Attrubute can only be applied to types not declarations

even though my ReminderRow is of enum type.

import UIKit

@unchecked
enum ReminderRow : Hashable, Sendable {
    case date
    case notes
    case time
    case title
    
    var imageName : String? {
        switch self {
            case .date: return "calendar.circle"
            case .notes: return "square.and.pencil"
            case .time: return "clock"
            default : return nil
        }
    }
    
    var image : UIImage? {
        guard let imageName else { return nil }
        let configuration = UIImage.SymbolConfiguration(textStyle: .headline)
        return UIImage(systemName: imageName, withConfiguration: configuration)
    }
    
    var textStyle : UIFont.TextStyle {
        switch self {
        case .title : return .headline
        default : return .subheadline
        }
    }
}

enum ReminderRow { ... } is the declaration of a type, not a type itself. You use the declared type later in variable declarations like let row: ReminderRow = .date. But @unchecked in general goes only in front of Sendable in an inheritance clause.

This error message is not very good in the circumstances. However, what it's trying to tell you is that here you have a declaration of a type (enum ReminderRow...) and you're not allowed to use @unchecked to decorate that.

It's not clear what you're trying to indicate by decorating the type itself as @unchecked--there is no such feature in Swift. We use @unchecked for conformances to Sendable to indicate to the compiler that you know the conforming type is Sendable via internal, manually implemented means that the compiler can't check. You would spell that as follows: enum ReminderRow: @unchecked Sendable.

4 Likes

Also FWIW, since the enum does not have any cases with associated values, it can be made Sendable without the use of @unchecked.

4 Likes

Actually i was trying to use this ReminderRow as itemIdentifier in the DataSource but according to New swift update itemIdentifier has to be hashable, sendable, even through i make it sendable i tried your solution as well but i was still getting this error “ReminderRow is Main actor-isolated” .
Btw at the end i make ReminderRow nonisolated and remove sendable now it work fine and swift implicitly make the ReminderRow sendable

import UIKit
import Foundation


enum ReminderRow : Hashable{
    case date
    case notes
    case time
    case title
    
    var imageName : String? {
        switch self {
            case .date: return "calendar.circle"
            case .notes: return "square.and.pencil"
            case .time: return "clock"
            default : return nil
        }
    }
    
    var image : UIImage? {
        guard let imageName else { return nil }
        let configuration = UIImage.SymbolConfiguration(textStyle: .headline)
        return UIImage(systemName: imageName, withConfiguration: configuration)
    }
    
    var textStyle : UIFont.TextStyle {
        switch self {
        case .title : return .headline
        default : return .subheadline
        }
    }
}