可能重复:
字符串文字与C中的const char*
为什么我能做到这一点?
void foo(char * cstr) {
...
}
Run Code Online (Sandbox Code Playgroud)
并在代码中
foo("some text");
Run Code Online (Sandbox Code Playgroud)
不应该"某些文本"是const char*类型?
如果它的char*,意味着我可以修改它?
有没有办法如何userInfo
NSDictionary
获得一个NSOperation
?
基本上我想为NSOperation分配一个ID,以后我想检查一下,如果这个ID已经分配给了NSOperation
- (void)processSmthForID:(NSString *)someID {
for (NSOperation * operation in self.precessQueue.operations) {
if ([operation.userInfo[@"id"] isEqualToString:someID]) {
// already doing this for this ID, no need to create another operation
return;
}
}
NSOperation * newOperation = ...
newOperation.userInfo[@"id"] = someID;
// enqueue and execute
}
Run Code Online (Sandbox Code Playgroud) 我想询问以下情况
int * foo() {
int fooint = 5;
return &fooint;
}
int myint = *foo();
Run Code Online (Sandbox Code Playgroud)
它基于 http://www.functionx.com/cpp/examples/returnpointer.htm
但我想问,如果它是安全的,因为我认为会发生
做起来不是更好
int * foo() {
int * fooint = new int;
*fooint = 5;
return fount;
}
int * tmp = foo();
int myint = * tmp;
delete tmp;
Run Code Online (Sandbox Code Playgroud) 这段代码好吗?
void SomeClass :: foo(const char * _name) {
//name is of type const char *
name = std::string(_name).c_str();
}
Run Code Online (Sandbox Code Playgroud)
它看起来像是在工作,但我不确定它是否正常
我应该做一个旧学校strcpy吗?
如果我在.app
捆绑包中保存文件,它会保存正常,但Apple建议将文件保存在Application Support下
~/Library/Application Support/appname
Run Code Online (Sandbox Code Playgroud)
要么
~/Library/Application Support/bundleid
Run Code Online (Sandbox Code Playgroud)
我试过两个,但我总是得到一个例外.我正在获得应用程序支持的路径,即
/Users/myname/Library/Application Support/com.company.appname/
Run Code Online (Sandbox Code Playgroud)
要么
/Users/myname/Library/Application Support/AppName/
Run Code Online (Sandbox Code Playgroud)
com.company.appname
在我的内部正确指定info.plist
,AppName
是product AppName.app
,所以路径似乎是正确的.
{
FilepathProcessor::pathForFile(fpath, "menustate", ".sav",
PATH_TO_DESTINATION_SAVE_LOAD_FOLDER);
std::ofstream file;
file.exceptions(std::ofstream::eofbit | std::ofstream::failbit |
std::ofstream::badbit);
INFO_ARG("prepare to save to '%s'", fpath.c_str());
try {
file.open(fpath.c_str(), std::ios::out | std::ios::binary |
std::ios::trunc);
ERROR_IF_ARG(!file.is_open(), "couldnt open file for saving at '%s'",
fpath.c_str(), return);
//will pass this point
//exception happens by first write
WRITE_INT_TO_BINFILE(file, episode);
//...
}
catch (std::ofstream::failure e) {
ERROR_IF_ARG(true, "exception %s", …
Run Code Online (Sandbox Code Playgroud) 如何获得一周的第一天约会
这似乎更容易,因为:
输入日期是一周中的任何日期与时间...我尝试了几种方法,但边缘情况使它难以实现
我做了一个函数,然而它不能100%工作(不确定[components setDay:-weekday + 2];)
- (NSDate *)firstDateOfWeek {
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
// because sunday is 0, we need it to be 6
NSInteger weekday = (([weekdayComponents weekday] + 6) % 7);
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
[components setDay: -weekday + 2];
return [calendar dateFromComponents:components];
}
Run Code Online (Sandbox Code Playgroud) 如何[super methodName...]
在重写方法中强制执行调用?
除了在评论中陈述它,还有另一种方式,也许是一招?
自 iOS14 以来,我们的应用程序显示了读取剪贴板通知,但是我们没有读取任何内容,这一定是某些 SDK(可能是 Facebook)的结果
但是如何在访问剪贴板时确认或断点,以找出从剪贴板读取的内容?
我有一个完整的静态类,使用std :: map
这就是简化的案例
.H
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <map>
class Keyboad {
static std::map<char, bool> pressed;
static void keyPressedEvent(char _key);
};
#endif
Run Code Online (Sandbox Code Playgroud)
的.cpp
#include "Keyboard.h"
void Keyboard :: keyPressedEvent(char _key) {
Keyboard :: pressed[_key] = true;
}
Run Code Online (Sandbox Code Playgroud)
但是静态成员变量存在问题,因为我得到了
Undefined symbols:
"Keyboard::pressed", referenced from:
__ZN15Keyboard7pressedE$non_lazy_ptr in Keyboard.o
(maybe you meant: __ZN15Keyboard7pressedE$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
当我删除它,它运行正常
为什么我会遇到这个问题,使用静态变量时应该没有问题:/
谢谢
c++ ×5
ios ×3
macos ×2
std ×2
char ×1
cocoa ×1
cocoa-touch ×1
debugging ×1
file ×1
nscalendar ×1
nsdate ×1
nsoperation ×1
objective-c ×1
string ×1
swift ×1
xcode ×1