我在Objective C中有一个编码,想要创建一个基本Controller并使用多态来创建子类.我也在关注CS193p stanford课程.由于多态性使用不同类的通用方法,如果xcode具有相同的函数名,xcode如何知道每个子类要调用哪个函数?
此外,在基类中调用一个函数但返回nil并且有一个注释说抽象.此函数在子类控制器中实现并返回一个值.如果基类控制器在层次结构中较低,它将如何调用子类函数?为什么不返回零?
我有一个充满NSStrings的数组,我需要使用NSString来更改属性文本的颜色.我把原来的NSString分成了一个包含单个NSString颜色的数组,但我被困在这里.数组中的字符串是[UIColor orangecolor].我试过这个,但它返回null.
SEL blackSel = NSSelectorFromString(@"blackColor");
UIColor* tColor = nil;
if ([UIColor respondsToSelector: blackSel])
tColor = [UIColor performSelector:blackSel];
Run Code Online (Sandbox Code Playgroud) 我想把if语句放在我的for循环检查参数中,但我不知道怎么做,我想要这样,所以我不必复制我的代码并使其变得庞大,冗余和混乱.
这就是我想要做的
for( int i = elevator1CurrentStatus.floorNumber; if(boundary == 9) i<boundary else i>boundary ;i+=i+countDirection){
//Code Here
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现?基本上我想要测试语句说明是否向上或向下计数以依赖变量并根据该变量选择哪个方向.计数方向较早实现,为+1或-1.for(int i = elevator1CurrentStatus.floorNumber;(if(boundary == 9)iboundary;); i + = i + countDirection)
for( int i = elevator1CurrentStatus.floorNumber; (if(boundary == 9) i<boundary else i>boundary) ;i+=i+countDirection)
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算文件中的单词数量,我知道这个问题已经被问过,但我已经尝试了一些我见过的实现,但是我一直收到错误.
我正在阅读的文件中的行是"Super Chill",但是当我运行代码时,我得到一个计数3,其中>>第一次获得Super,然后两次获得Chill.关于这种方法,我有几个问题:
1)While(in)寻找什么?怎么知道什么时候停止?
2)为什么"寒意"用>>存储两次?
这是代码
int countWords(std::istream& in){ // line in file is -> Super Chill
int count = 0;
std::string word;
while (in) {
in >> word;
if (word != "") {
count+= 1;
}
}
return count;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用集合计算素数,但是当我进行计算时,我的迭代器随机跳跃.
我试图为N = 10的值实现此方法.
选择一个整数n.此函数将计算最多n的所有素数.首先将1到n中的所有数字插入到集合中.然后擦除2的所有倍数(2除外); 也就是说,4,6,8,10,12 ......消除所有3的倍数,即6,9,12,15 ....... 上升到sqrt(n).剩下的数字都是素数.
当我运行我的代码时,它会删除1然后pos跳转到4?我不确定为什么会发生这种情况,而不是它转到值2,这是集合中的第二个值?
在我擦除迭代器指向的值之后会发生什么,迭代器指向的是什么以及如果我将它推进到哪里前进?
这是代码:
set<int> sieveofEratosthenes(int n){ //n = 10
set<int> a;
set<int>::iterator pos = a.begin();
//generate set of values 1-10
for (int i = 1; i <= n; i++) {
a.insert(i);
if(pos != a.end())
pos++;
}
pos = a.begin();
//remove prime numbers
while (pos != a.end())
{
cout << "\nNew Iteration \n\n";
for (int i = 1; i < sqrt(n); i++) {
int val = *pos%i;
cout << "Pos = " …Run Code Online (Sandbox Code Playgroud) 我有类,一个Employee类和一个BankAccount类,employee类将BankAccount类作为私有变量指针.
这就是我想要做的:
BankAccount在每个Employee值中设置所有sBankAccounts每一个Employee在函数结束.我使用成员函数setter Employee来设置BankAccount指针.BankAccount有一个私有变量,这是金额.后来我在一个指向每个BankAccount's内存地址的指针上调用delete .在我打电话给印刷品以查看每个银行的银行值之后Employee,它仍然是每个的打印值BankAccount
如果我调用delete不应该删除堆上的内存并且调用print时不输出任何内容BankAccount?
这是代码:
vector<Employee*> employees;
//get employee full name & salary and return
employees.push_back(get_employee_info());
//setup their bank account
setup_bank(employees);
//make temp pointer to store bank memory address
BankAccount * tempBankPtr;
for (int i =0; i < employees.size(); i++) {
tempBankPtr =employees[i]->get_bank();
delete tempBankPtr // delete the heap object …Run Code Online (Sandbox Code Playgroud) c++ ×4
objective-c ×2
pointers ×2
count ×1
file ×1
for-loop ×1
heap ×1
if-statement ×1
iterator ×1
memory ×1
nsstring ×1
polymorphism ×1
set ×1
stream ×1
uicolor ×1