在.h中放置一个实例变量有什么用,它没有getter和setter(即没有人可以设置也不能得到它)? [英] What is the use of placing an instance variable in .h where it wouldn't have a getter and setter ( i.e. nobody can set nor get it)?

查看:151
本文介绍了在.h中放置一个实例变量有什么用,它没有getter和setter(即没有人可以设置也不能得到它)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读将iVars置于现代的位置。 Objective-C?和其他一些问题,但我仍然感到困惑。

I have read Where to put iVars in "modern" Objective-C? and some other questions, but I am still confused.

我正在阅读 https://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-应用 SQLite教程:

I am reading from https://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app SQLite Tutorial:

.h

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface FailedBankDatabase : NSObject {
sqlite3 *_database; 
}

+ (FailedBankDatabase*)database;
- (NSArray *)failedBankInfos;

@end

.m

#import "FailedBankDatabase.h"
#import "FailedBankInfo.h"

@implementation FailedBankDatabase

static FailedBankDatabase *_database;

+ (FailedBankDatabase*)database {
    if (_database == nil) {
        _database = [[FailedBankDatabase alloc] init];
    }
    return _database;
}

请更正我错的地方:
sqlite3 *数据库


  • 不是属性

  • 没有任何合成

  • .m文件中没有任何setter或getter,因此没有其他类可以访问它 - 无法放入 .h file

  • Isn't a property
  • Doesn't have any synthesize
  • Doesn't have any setter or getter in .m file so no other class can access it--defeating the purpose of placing in .h file

我们无法访问或设置所有含义! 我们为什么要这样做?

All meaning we can't access nor set it! Why are we doing this?

将此信息放在 .h中的目的是什么档案;为什么不把
写成属性
或者只是单独写入我们的 .m 文件?

What what is the purpose of placing this in .h file; Why not just write as a property? Or just write in our .m file alone?

编辑:
在Objective-C中使用Sqlite3的现代方法是什么?

What is the modern way of using Sqlite3 in Objective-C?

推荐答案

并非总是如此可以在实现块中声明实例变量。这可能已经有好几年了,但该教程的日期是2010年4月8日。撰写本文时,作者可能希望它能够在旧版本的编译器上运行。

It was not always possible to declare instance variables in the implementation block. It has been possible for several years, but the date of that tutorial is April 8, 2010. When it was written, the author probably wanted it to work on older versions of the compiler.

因为 _database 未声明 @private ,实际上可以访问外部的实例变量 FailedBankDatabase 实现。在Objective-C中,您可以将对象指针视为结构指针并访问其实例变量,如struct fields。所以你可以这样做:

Because _database is not declared @private, it is in fact possible to access the instance variable outside of the FailedBankDatabase implementation. In Objective-C, you can treat an object pointer as a struct pointer and access its instance variables like struct fields. So you could do this:

#import "FailedBankDatabase.h"

int main(int argc, char *argv[])
{
    FailedBankDatabase *db = [FailedBankDatabase database];
    printf("instance variable: %p\n", db->_database);
    return 0;
}

但这可能是一个坏主意。

That's probably a bad idea, though.

由于您的 sqlite3 * _database 是私有的,您可能只是想要这样做:

Since your sqlite3 *_database is intended to be private, you probably just want to do this:

#import <Foundation/Foundation.h>

@class FailedBankInfo;

@interface FailedBankDatabase : NSObject

+ (FailedBankDatabase*)database;
- (NSArray<FailedBankInfo *> *)failedBankInfos;

@end



FailedBankDatabase.m



FailedBankDatabase.m

#import "FailedBankDatabase.h"
#import "FailedBankInfo.h"
#import <sqlite3.h>

@implementation FailedBankDatabase {
    sqlite3 *_database;
}

static FailedBankDatabase *theInstance;

+ (FailedBankDatabase*)database {
    if (theInstance == nil) {
        theInstance = [[FailedBankDatabase alloc] init];
    }
    return theInstance;
}

这篇关于在.h中放置一个实例变量有什么用,它没有getter和setter(即没有人可以设置也不能得到它)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆