gpt4 book ai didi

objective-c - iOS 中的 c 结构实现

转载 作者:行者123 更新时间:2023-11-28 22:51:30 27 4
gpt4 key购买 nike

SSharedAppState.h

#import <Foundation/Foundation.h>

enum {
SharedCompletedStepNone = 0,
SharedCompletedStepOne = 1 << 0,
SharedCompletedStepTwo = 2 << 1,
SharedCompletedStepThree = 3 << 2
};
typedef NSUInteger SharedCompletedSteps;

@interface SSharedAppState : NSObject
{
struct {
unsigned int sharedCompletedStepMask:3;
} _appFlags;
}
@property (assign, nonatomic) SharedCompletedSteps sharedCompletedStep;

+(id)sharedInstance;
@end

SSharedAppState.m

#import "SSharedAppState.h"

#pragma mark - C functions


#pragma mark - Interface Extension

typedef struct _appFlags AppFlags;
@interface SSharedAppState ()

@property (unsafe_unretained, nonatomic) AppFlags *appFlags;
-(void *)newAppFlags;
@end

#pragma mark - Implementation

@implementation SSharedAppState
@synthesize appFlags;

#pragma mark - Iniitalizer

-(id)init
{
self = [super init];
if (self) {

// appFlags = (AppFlags *)malloc(sizeof(_appFlags));
appFlags = (AppFlags *)newAppFlags();
appFlags->sharedCompletedStepMask = 0;
appFlags.sharedCompletedStepMask = 0;
}
return self;
}

+(SSharedAppState *)sharedInstance
{
static dispatch_once_t onceToken;
static id sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}

#pragma makr - Struct _appFlags

-(void *)newAppFlags
{
AppFlags *instance = (AppFlags *)malloc(sizeof(_appFlags));
return instance;
}


@end

问题

  1. appFlags = (AppFlags *)newAppFlags();

给出函数 newAppFlags 的隐式声明在 C99 中无效

  1. appFlags->sharedCompletedStepMask = 0;appFlags.sharedCompletedStepMask = 0;

类型“struct _appFlags”的不完整定义成员引用类型“AppFlags *”(又名“struct _appFlags *”)是一个指针;也许您打算使用“->”?

给出以下警告和错误。这是我第一次在这里发布问题,所以请就 html 或编辑提出建议。谢谢!

最佳答案

当你这样写的时候

struct {
unsigned int sharedCompletedStepMask:3;
} _appFlags;

您正在声明一个未命名结构的变量 _appFlags。

你可能是说

struct _appFlags {
unsigned int sharedCompletedStepMask:3;
};

或者你可以这样写

typedef struct _appFlags {
unsigned int sharedCompletedStepMask:3;
} AppFlags;

关于objective-c - iOS 中的 c 结构实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com