如果我在Objective-C中声明类变量,何时释放内存?
如果我的界面是:
@interface TestClass : NSObject
{
}
+ (NSString)instanceCount;
@end
Run Code Online (Sandbox Code Playgroud)
在实施中,我宣布:
static NSString instanceCount;
Run Code Online (Sandbox Code Playgroud)
如何发布此类级别变量?即什么时候dealloc在Objective-C中调用类变量?
我有点时间看似相当
简单的东西,但我似乎无法工作.我正在构建一个
从Web主机检索数据的iPhone应用程序.我正在尝试
建立与主机的异步连接,因为我想
在连接期间保持设备释放.(sendSynchronousRequest
将手机冻结,直到请求完成.)这是我的连接代码:
//temp url to see if data is returned:
NSURL *theURL = [NSURL URLWithString:@"http://www.theappleblog.com/feed"];
NSURLRequest *dataRequest = [NSURLRequest requestWithURL:theURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
/* establish the connection */
theConnection = [[NSURLConnection alloc]
initWithRequest:dataRequest
delegate:self
startImmediately:YES];
if (theConnection == nil) {
NSLog(@"Connection Failure!");
self.urlData = nil;
} else {
self.urlData = [[NSMutableData data] retain];
}
Run Code Online (Sandbox Code Playgroud)
我已经设置了所有适当的委托方法:
-(void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse*)response
{
[urlData setLength:0];
UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = YES;
NSLog(@"Received Response!");
}
Run Code Online (Sandbox Code Playgroud)
-(void)connection:(NSURLConnection *)connection
didReceiveData:(NSData*)incrementalData
{
[self.urlData …Run Code Online (Sandbox Code Playgroud) 简短版本:
release当Cocoa应用程序终止时,为什么NSView对象的子视图没有发送消息?
示例:
下面MyView显示的类只不过NSView是在创建和销毁时向控制台报告的子类.我测试了它,发现它正常工作.但是,当我使用它时,如我的应用程序委托的下一个代码片段所示,我看到一些意外的事情(请参阅示例输出).
// MyView:
@interface MyView : NSView { }
@end
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) == nil) { return nil; }
NSLog(@"init %@", self);
return self;
}
- (void)dealloc
{
NSLog(@"dealloc %@", self);
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
// Application delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"begin");
parentView = [[MyView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
MyView * myView = [[MyView alloc] initWithFrame:NSMakeRect(10, 10, 80, 80)];
[parentView addSubview:myView];
[myView …Run Code Online (Sandbox Code Playgroud) 我刚写了以下代码行:
if (++(data_ptr->count) > threshold) { /*...*/ } // example 1
Run Code Online (Sandbox Code Playgroud)
我的目的是在进行比较之前增加count数据结构中data_ptr指向的变量threshold,并且这行代码有效.
如果我data_ptr在进行比较之前想要增加,我会写这个:
if ((++data_ptr)->count > threshold) { /*...*/ } // example 2
Run Code Online (Sandbox Code Playgroud)
出于好奇,我也尝试了这行代码:
if (++data_ptr->count > threshold) { /*...*/ } // example 3
Run Code Online (Sandbox Code Playgroud)
并发现它的行为与第一个完全相同.
第一个问题: 为什么示例#3与示例#1的作用相同?这是运营商优先考虑的问题吗?标准中的东西?我不得不写一个快速的测试程序,因为答案对我来说并不明显.
第二个问题:我应该以if不同的方式撰写此声明 我可以先在自己的行上执行增量,然后测试条件以避免任何可能的混淆.这是必要的,或者前两个例子是否足够明显?
我正在开发一个简单的CAD程序,它使用OpenGL来处理屏幕渲染.屏幕上绘制的每个形状完全由简单的线段构成,因此即使是简单的绘图也最终会处理数千条单独的线条.
在我的应用程序和OpenGL之间传递此行集合中的更改的最佳方法是什么?有没有办法只更新OpenGL缓冲区中某些行的子集?
我在这里寻找一个概念性的答案.无需深入了解实际的源代码,只需要了解有关数据结构和通信的一些建议.
我看到一些带有析构函数的C++类定义如下:
class someClass
{
public:
someClass();
~someClass() throw();
};
Run Code Online (Sandbox Code Playgroud)
这是一个好主意吗?
我很清楚析构函数应该永远不会抛出异常,但这实际上会阻止我在析构函数中抛出异常吗?我不是100%肯定它保证什么.
参考:最近这个问题
在下面的C++代码中,我意识到gcount()返回的数字比我想要的更多,因为它getline()消耗了最终的换行符,但是没有将它发送到输入流.
但我仍然不明白的是程序的输出.对于输入"Test \n",为什么我得到"est \n"?为什么我的错误会影响字符串的第一个字符,而不是在末尾添加不需要的垃圾?为什么程序的输出与字符串在调试器中的显示方式不一致("Test \n",正如我所期望的那样)?
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
const int bufferSize = 1024;
ifstream input( "test.txt", ios::in | ios::binary );
vector<char> vecBuffer( bufferSize );
input.getline( &vecBuffer[0], bufferSize );
string strResult( vecBuffer.begin(), vecBuffer.begin() + input.gcount() );
cout << strResult << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当一个对象在另一个对象上调用方法时,Objective-C使用复杂的消息传递系统.我想知道在被调用的方法中是否有可能确定调用对象是什么?
例如:
@implementation callingClass
- (void)performTest
{
calledObject = [[[calledClass alloc] init] autorelease];
id result = [calledObject calledMethod];
assert(result == this);
}
@end
@implementation calledClass
- (id)calledMethod
{
id objectThatCalledThisMethod = ... // <-- what goes here?
return objectThatCalledThisMethod;
}
@end
Run Code Online (Sandbox Code Playgroud)
我可以在注释行中写什么,以便在执行时使断言通过performTest?
Apple已弃用OS X v10.5及更高版本NSObject的poseAsClass:方法.有没有其他方法让班级合法?
默认情况下,Xcode会自动将C样式注释块中的多行代码缩进一个空格:
/* this is a comment block
line 1
line 2
*/
Run Code Online (Sandbox Code Playgroud)
是否可以修改此行为?我宁愿在评论块中没有缩进.
objective-c ×4
c++ ×2
cocoa ×2
asynchronous ×1
c ×1
class ×1
cocoa-touch ×1
comments ×1
conceptual ×1
cout ×1
deprecated ×1
dereference ×1
destructor ×1
exception ×1
increment ×1
indentation ×1
iphone ×1
methods ×1
nsview ×1
opengl ×1
operators ×1
performance ×1
runtime ×1
static ×1
string ×1
variables ×1
vector ×1
xcode ×1