【问题标题】:Struct in Objective c目标c中的结构
【发布时间】:2011-07-06 05:32:03
【问题描述】:

我需要在objective c中创建四个Struct

他们是:

typedef struct FILE_TRANSFER_REQUEST_STRUCT
{
    uint8_t header[16];  // This would be MD5 of String "SymbolTalk"
    uint8_t type;        // Type of structure -> FILE_TRANSFER_REQUEST
    char    fileName[512];
    int     totalSize; //4096
    int     chunkSize; //1024
    int     numChunks; // 4
}FileTransferRequest;

typedef struct FILE_TRANSFER_RESPONSE_STRUCT
{
    uint8_t header[16];  // This would be MD5 of String "SymbolTalk"
    uint8_t type;        // Type of structure -> FILE_TRANSFER_RESPONSE
    ;
}FileTransferResponse;

typedef struct FILE_CHUNK_REQUEST_STRUCT
{
    uint8_t header[16];  // This would be MD5 of String "SymbolTalk"
    uint8_t type;        // Type of structure -> FILE_CHUNK_REQUEST

    uint8_t data[1024];
    uint8_t chunkNumber;
    uint8_t chunkSize;
}FileChunkRequest;

typedef struct FILE_CHUNK_RESPONSE_STRUCT
{
    uint8_t header[16];  // This would be MD5 of String "SymbolTalk"
    uint8_t type;        // Type of structure -> FILE_CHUNK_RESPONSE
    uint8_t chunkNumber;

}FileChunkResponse;

但是我不知道目标结构。我该怎么做。我需要在哪里创建这个结构(需要避免类)。它是否写在目标 c 类文件中。谁能告诉我我怎么能得到这个结构在另一个类中..任何人都可以帮助我...

【问题讨论】:

  • 对我来说看起来不错。什么不起作用?

标签: iphone objective-c xcode struct


【解决方案1】:

您可以像在普通 C 中一样执行此操作。将它们放入您的 .h 文件之一(对于它们的用途最有意义的一个),在您的 @interface 块之外.这样他们就不会成为任何班级的一部分。

要在类中使用它们,只需#import 包含结构定义的 .h 文件。

【讨论】:

    【解决方案2】:

    就这样用吧……

    typedef struct FILE_CHUNK_RESPONSE_STRUCT
    {
        uint8_t header[16];  // This would be MD5 of String "SymbolTalk"
        uint8_t type;        // Type of structure -> FILE_CHUNK_RESPONSE
        uint8_t chunkNumber;
    
    }FileChunkResponse;
    
    @class sampleViewController;
    
    @interface sampleAppDelegate : NSObject <UIApplicationDelegate> {
       struct FILE_CHUNK_RESPONSE_STRUCT mystructure;
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    
    @property (nonatomic, retain) IBOutlet sampleViewController *viewController;
    
    @end
    

    Objective-C 是 c 的超集,它支持所有 c 代码...如果您想使用 C++ 相关类,请将文件名更改为 .mm 而不是 .m

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2021-04-07
      • 2011-06-14
      • 2010-11-26
      • 2014-05-18
      • 2011-08-22
      相关资源
      最近更新 更多