我是Objective-C的新手,但我很好奇我在其他任何地方都没有看过的东西.
谁能告诉我在@interface块中声明的私有变量与@implementation在类方法之外的块中声明的变量之间的区别是什么,即:
@interface Someclass : NSObject {
NSString *forExample;
}
@end
Run Code Online (Sandbox Code Playgroud)
与
@implementation Someclass
NSString *anotherExample;
-(void)methodsAndSuch {}
@end
Run Code Online (Sandbox Code Playgroud)
似乎两个变量(forExample,anotherExample)在整个班级中都可以同等地访问,我无法真正发现他们的行为有所不同.第二种形式也称为实例变量吗?
对于这个例子,我正在使用objective-c,但欢迎来自更广泛的C/C++社区的答案.
@interface BSWidget : NSObject {
float tre[3];
}
@property(assign) float* tre;
Run Code Online (Sandbox Code Playgroud)
.
- (void)assignToTre:(float*)triplet {
tre[0] = triplet[0];
tre[1] = triplet[1];
tre[2] = triplet[2];
}
Run Code Online (Sandbox Code Playgroud)
.
- (void)copyToTre:(float*)triplet {
memcpy(tre, triplet, sizeof(tre) );
}
Run Code Online (Sandbox Code Playgroud)
所以在这两种方法之间,并考虑到这些setter函数通常只能处理2,3或4的维数......
对于这种情况,最有效的方法是什么?
gcc一般会将这些减少到相同的基本操作吗?
谢谢.
请考虑以下陈述:
int *pFarr, *pVarr;
int farr[3] = {11,22,33};
int varr[3] = {7,8,9};
pFarr = &(farr[0]);
pVarr = varr;
Run Code Online (Sandbox Code Playgroud)
在这个阶段,两个指针都指向每个相应阵列地址的开头.对于*pFarr,我们目前正在考虑11和*pVarr,7.
同样,如果我通过*farr和*varr请求每个数组的内容,我也得到11和7.
到现在为止还挺好.
现在,让我们尝试pFarr++和pVarr++.大.正如预期的那样,我们现在看22和8.
但现在...
试图拉升farr++和varr++...我们得到"错误类型的参数来增加".
现在,我认识到数组指针和常规指针之间的区别,但由于它们的行为相似,为什么会出现这种限制呢?
当我还考虑在同一个程序中我可以用表面上正确的方式和另一种不正确的方式调用以下函数时,这让我更加困惑,我得到了相同的行为,尽管与上面发布的代码中发生的情况形成对比!?
working_on_pointers ( pFarr, farr ); // calling with expected parameters
working_on_pointers ( farr, pFarr ); // calling with inverted parameters
Run Code Online (Sandbox Code Playgroud)
.
void working_on_pointers ( int *pExpect, int aExpect[] ) {
printf("%i", *pExpect); // displays the contents of pExpect ok
printf("%i", *aExpect); // displays the contents of …Run Code Online (Sandbox Code Playgroud) 我原本以为我在整个视图控制器模型上实际上有一个很好的处理,但对我来说似乎没有什么意义.我的主要问题是添加自定义UIView子类作为UIViewController子类的属性.
每当我将UIView子类的有效实例分配给该属性时,没有任何反应或代码崩溃.
这是一个快速概述:
addSubview:ivar等将这个UIView子类添加到主控制器中.那里没有问题...但是...如果我想将这个自定义UIView作为ViewController 的属性,那似乎不起作用.任何人都能解释一下吗?
这是一个代码摘要:
@interface CustomUIView : UIView { }
Run Code Online (Sandbox Code Playgroud)
.
@interface MainViewController : UIViewController {
CustomUIView *someOtherView;
}
@property (nonatomic, copy) CustomUIView *someOtherView;
Run Code Online (Sandbox Code Playgroud)
...
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor]; // the default controller view
CustomUIView *tmpView = [[CustomUIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:tmpView]; // this works
self.someOtherView = tmpView; // this does NOT work and
self.view = self.someOtherView; // ultimately, this is what i'm after
[tmpView …Run Code Online (Sandbox Code Playgroud) 我正在尝试优化代码的性能,但我一般不熟悉xcode的调试器或调试器.是否可以跟踪在运行时进行的调用的执行时间和频率?
想象一段事件链,在几分之一秒内有一些递归调用.跟踪CPU花费大部分时间的最佳方法是什么?
非常感谢.
编辑:也许最好问一下,如何使用xcode调试工具进行堆栈跟踪?
objective-c ×3
arrays ×2
c ×2
iphone ×2
debugging ×1
memcpy ×1
optimization ×1
performance ×1
pointers ×1
properties ×1
uiview ×1
xcode ×1