来自"目标C中的编程"(Kochan):
程序5.8提示用户输入一个数字,然后继续显示从最右边到最左边数字的数字.
// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit;
NSLog (@"Enter your number.");
scanf ("%i", &number);
while ( number != 0 ) {
right_digit = number % 10;
NSLog (@"%i", right_digit);
number /= 10;
}
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:当用户输入单位数字1到9时会发生什么?我找不到关于这种情况的任何材料.编译后,程序继续返回该单个数字.这是为什么?我试图为这项任务提出代码并花费2个小时,尝试为这个"如果数字是单个数字"问题合并循环和决策.而解决方案是如此无知!
我正在从"Objective-objective编程"作者Kochan那里学习Objective-c.第3版.在第8章"继承"中,Kochan先生对该方法给出了以下解释:
-(void) setOrigin: (XYPoint *) pt
{
if (! origin)
origin = [[XYPoint alloc] init];
origin.x = pt.x;
origin.y = pt.y;
}
Run Code Online (Sandbox Code Playgroud)
" 该方法首先测试实例变量origin是否为非零(确保您理解该测试并使用逻辑否定运算符!已使用).回想一下,所有实例变量最初都设置为零.所以当一个新的Rectangle对象被分配,其实例变量(包括origin)将被设置为零.
如果原点为零,则setOrigin:方法将分配并初始化一个新的XYPoint对象,并在原点中存储对它的引用."
有逻辑错误吗?仅当原点不为零时,"setOrigin"方法才会分配新的XYPoint对象吗?
尝试从不同的方式解决这个问题。当前的实现给了我一个错误:“NSInvalidArgumentException”,原因:“索引 0 部分中索引 3 处没有对象”它发生在 AppDelegate 类:UIResponder...
我的代码是
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
configureCell(cell: cell, indexPath: indexPath)
var numberOfItems = self.collectionView(self.collectionView, numberOfItemsInSection: 0)
if (indexPath.row == numberOfItems - 1) {
var addCellButton = UIButton(frame: cell.frame)
addCellButton.setTitle("Add", for: UIControlState.normal)
cell.addSubview(addCellButton)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let sections = controller.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects + …Run Code Online (Sandbox Code Playgroud)