问题标签 [declared-property]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
delegates - Xcode iPhone SDK (5.x) - 在 AppDelegate 中访问 NSString
我想NSString
从AppDelegate.m
位于内部ViewController.m
的 .
我有一个单一视图应用程序,我想将我的NSString
使用保存applicationDidEnterBackground:
在里面AppDelegate.m
。
NSString
位于内部ViewController.m
,并且未在 中声明AppDelegate.m
。
我试图在其中声明它,AppDelegate.h
然后在ViewController.m
(并且反转)中访问它。
ViewController.h
:
AppDelegate.m
:
objective-c - Is there a pattern to override a property?
The Objective-C runtime keeps a list of declared properties as meta-data with a Class object. The meta-data includes property name, type, and attributes. The runtime library also provides a couple of functions to retrieve these information. It means a declared property is more than a pair of accessor methods (getter/setter). My first question is: Why we (or the runtime) need the meta-data?
As is well known, a declared property cannot be overridden in subclasses (except readwrite vs. readonly). But I have a scenario that guarantees that needs:
Of course, the compiler won't let the above code pass through. My solution is to substitute the declared property with a pair of accessor methods (with the readonly case, just the getter):
Property string
needs to be mutable because it is modified incrementally and potentially frequently. I know the constraint that methods with the same selector should share the same return and parameter types. But I think the above solution is appropriate both semantically and technically. For the semantic aspect, a mutable object is a immutable object. For the technical aspect, the compiler encodes all objects as id's. My second question is: Does the above solution make sense? Or it's just odd?
I can also take a hybrid approach, as follows:
However, when I access the property using the dot syntax like myMutableObject.string
, the compiler warns that the return type of the accessor method does not match the type of the declared property. It's OK to use the message form as [myMutableObject string]
. That suggests another aspect where a declared property is more than a pair of accessor methods, that is, more static type checking, although it is undesirable here. My third question is: Is it common to use getter/setter pair instead of declared property when it is intended to be overridden in subclasses?
objective-c - ARC 下的 NSString 属性应该是强的还是复制的?
不使用 ARC 编译时,建议使用copy
数据类型的属性,例如NSString
. 我找不到关于copy
在 ARC 模式下使用的正确文档。有人能告诉我什么适用于 ARC 吗?
objective-c - 通过弱指针在块中分配 ivar
isFinished
我的接口文件中有一个只读属性:
我想YES
稍后将其设置为一个块,而不创建保留周期self
:
我不确定这是不是正确的方法。这段代码会泄漏、损坏还是没问题?也许我忽略了一种更简单的方法?
objective-c - 何时对属性使用强或弱
我有一个表格视图IBOutlet
,默认情况下 XCode 将其属性设置为strong
而不是weak
. 有时我会收到“收到内存警告”消息。所以我尝试将许多属性从strong
to更改weak
,但它似乎并没有影响过程并且事情运行顺利。我应该将出口设置为弱,还是我错了?
最重要的是,我应该在方法中设置所有属性,还是只设置s?nil
viewDidUnload
IBOutlet
objective-c - 如何在保留合成代码的同时将一些代码添加到合成的 getter/setter
例如,我需要在NSLog(@"Executed.")
每次调用合成的 getter 或 setter 时执行。我看到了两种方法:
- 找到一些可能像合成的片段一样工作的片段。该线程可能对此有所帮助。
- 使用 KVO:添加一些可以完成工作的观察者。
对我来说,所有这些看起来都不令人满意。所以,主题。
UPDAE:感谢您的回答,但直接覆盖不是解决方案:我们丢失了合成代码。如果我们从某个地方“复制粘贴”“正确”合成代码(甚至来自苹果工程师给我们代码的苹果论坛),我们应该检查它在下一次编译器发布后没有改变。
objective-c - Apple LLVM 4.0 新的“默认合成”功能如何运作?
我正在浏览 Xcode 4.4 的发行说明并注意到这一点:
LLVM 4.0 编译器
Xcode 现在包括 Apple LLVM Compiler 4.0 版,包括以下新的Objective-C 语言功能:
- 默认@synthesize:未实现时自动合成一个@property
我对这个功能很感兴趣。它是如何工作的?我试过删了@synthesize
,还是不行。
objective-c - 尝试使用复制的 NSMutableString 属性会导致异常
我开始了一个小型 Xcode 项目来调查一个NSMutableString
属性是否应该是copy
或retain
. 我用属性声明了我的copy
属性:
然后将其初始化为self.stringA = [NSMutableString new];
最后尝试设置一个字符串[stringA setString:@"A"];
。
然而,该程序给出,
“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'尝试使用 setString 改变不可变对象:'”
是因为结果字符串是 aNSString
吗?这是否意味着我应该NSMutableString
使用retain
属性和NSString
属性声明我的属性copy
?
objective-c - 关于ivars范围的快速查询
这些 ivars的作用域(@private
、@protected
等)是什么?
-@property/@synthesize
- 声明于.h
- 声明于.m
objective-c - 为什么 setter 被调用一个属性而不是另一个?
我从 Big Nerd Ranch iOS Programming 一书中获取了这段代码。在代码中,他们分配了两个实例变量,coordinate
并且title
. 为什么coordinate
直接赋值,title
调用setter来设置?
头文件
实施文件