我真的需要一些帮助...
当我尝试使用 NSMutableArray 获取部分的单元格数时,我得到 [__NSCFString count]: unrecognized selector sent to instance 0x6d567e0
当我使用“rows = [[searchProfile objectAtIndex:section] count];”时会发生这种情况
这是代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"1");
// _________ Return the number of sections ________
int sections = 0;
if (tableView == self.tableView)
{
sections = 2;
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
sections = [searchedProfile count];
}
return sections;
}
/* ------------------------------------------ Set The Number Of Cells for Each Section ------------------------------- */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"2");
// _________ Return the number …Run Code Online (Sandbox Code Playgroud) Mods:我意识到这是这个问题的重复,你能关闭/删除它吗?
我有这个数组,其中必须根据某些条件添加或不添加多个元素。通常我会做这样的事情:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:@"aaa"];
[myArray addObject:@"bbb"];
if (flag)
[myArray addObject:@"flag"];
if (box)
[myArray addObject:@"box"];
[myArray addObject:@"xxx"];
if (x > 5)
[myArray addObject:@"smaller"];
if ([self isMenuVisible])
[myArray addObject:@"menu"];
... etc.
Run Code Online (Sandbox Code Playgroud)
但这显得令人困惑和复杂。
我可以想象我可以创建几种方法来做到这一点,但这将涉及传递字典、对象和条件数组等,但所有这些都显得比这更蹩脚和更复杂。
你们如何以更优雅的方式做到这一点?
我已经搜索了一段时间但是找不到合理的答案.我想在一个数组中添加/删除对象,以便在指向第一个数组的第二个数组中生效.
class Person
{
var name:String = ""
}
var arr1:[Person] = [Person]()
let p1 = Person()
p1.name = "Umair"
let p2 = Person()
p2.name = "Ali"
arr1.append(p1)
arr1.append(p2)
var arr2 = arr1
print("\(arr1.count)") //"2\n"
print("\(arr2.count)") //"2\n"
arr1.removeFirst()
print("\(arr1.count)") //"1\n"
print("\(arr2.count)") //"2\n"
Run Code Online (Sandbox Code Playgroud)
为什么更改arr1不会影响arr2.请帮我完成这个.
以下是我的原始数组:
{
FilePath = "Selfcare.jpg";
ModuleId = 18;
ModuleName = "Infant N Toddler";
SkillLevelID = 59;
SkillLevelName = "Self Care";
},
{
FilePath = "Preschool.jpg";
ModuleId = 18;
ModuleName = "Infant N Toddler";
SkillLevelID = 60;
SkillLevelName = "Preschool Readiness";
},
{
FilePath = "Learning_Primary.jpg";
ModuleId = 22;
ModuleName = Cognitive;
SkillLevelID = 71;
SkillLevelName = "Learning Readiness - Primary Skills";
},
{
FilePath = "PreAcad_Primary.jpg";
ModuleId = 22;
ModuleName = Cognitive;
SkillLevelID = 72;
SkillLevelName = "Pre-Academics: Primary Skills"; …Run Code Online (Sandbox Code Playgroud) 我想在另一个数组的开头插入一个数组。我怎样才能在 Swift 中做到这一点?
基本上,当用户滚动到顶部时,我从服务器获取数据并希望在顶部而不是底部显示新数据。
这是我的代码:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 { // user reached the top
if (self.loadMore == true) {
self.page += 1
self.getUserData(Page:self.page)
}
}
}
func getUserData(Page:Int) {
DataService().getData(page:Page) { data, error, errorDic in
if error != nil {
//oops
return
}
if (self.loadMore == true) {
self.data += data!
}
self.tableView.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud) 关于这个问题我到处都看了.我要做的就是创建一个nsuser默认对象,然后向它添加一个可变数组并尝试修改nsuserdefaults对象.
//Created a sample array
NSMutableArray *xml=[[NSMutableArray alloc] init];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
//assigned to defaults object which is created previously with a mutableCopy
[defaults setObject:[xml mutableCopy] forKey:@"userLocationsDetailedXML"];
//Tried to modify the defaults object - which at this point should be mutable
[[defaults objectForKey:@"userLocationsDetailedXML" ] replaceObjectAtIndex:1 withObject:@"y"] ;
Run Code Online (Sandbox Code Playgroud)
添加也没有[defaults synchronize]帮助.
但令人惊讶的是,默认对象仍然是不可变的.它是__NSCFArray,而不是__NSCFArrayM如预期的任何建议?
编辑:控制台输出类:__ NSCFArray => 因为我创建了一个可变副本,所以它不应该是_ NSCFArrayM吗?[ _NSCFArray replaceObjectAtIndex:withObject:]:mutating方法发送到不可变对象'
开发ios应用程序.我有一个对象类Product.h和.m以及我的选择器类,它实现我的车辆选择以匹配产品到车辆.文件
product.h
#import <Foundation/Foundation.h>
@interface Product : NSObject {
}
@property (nonatomic, retain) NSString *iProductID;
@property (nonatomic, retain) NSString *vchProductCode;
@property (nonatomic, retain) NSString *vchPriceCode;
@property (nonatomic, retain) NSString *vchHitchUPC;
@property (nonatomic, retain) NSString *mHitchList;
@property (nonatomic, retain) NSString *mHitchMap;
@property (nonatomic, retain) NSString *fShippingWeight;
@property (nonatomic, retain) NSString *vchWC;
@property (nonatomic, retain) NSString *vchCapacity;
@property (nonatomic, retain) NSString *txtNote1;
@property (nonatomic, retain) NSString *txtNote2;
@property (nonatomic, retain) NSString *txtNote3;
@property (nonatomic, retain) NSString *txtNote4;
@property (nonatomic, retain) NSString *vchDrilling; …Run Code Online (Sandbox Code Playgroud) 所以这是我的代码:
[dataCenter.tempPalette replaceObjectAtIndex:9 withObject:selectedColour];
Run Code Online (Sandbox Code Playgroud)
对象9确实存在,它当前是一个int(不确定是否重要).selectedColour也是一个int.dataCenter.tempPalette是一个NSMutableArray.
它给我的错误是这样的:
传递'replaceObjectsAtIndex:withObject:'的参数2使得指针来自整数而没有强制转换.
有任何想法吗?
我试图理解从一个NSMutableArray复制对象到另一个NSMutableArray.请考虑以下两种情况:1 - 将原始文件复制到克隆,其中克隆中的更改将影响原始文件.2 - 将原件复制到克隆,其中关闭中的更改不会影响原件.
首先,我尝试使用以下代码首先生成场景#1.根据我的理解,当复制数组不使用'mutablecopy'时,克隆数组将只保存指向原始字符串对象的指针.因此,如果我要将克隆的第一个元素更改为另一个对象,原始的第一个元素会改变得太对吗?......但这不是我得到的结果.为什么?
事实上,当我使用mutablecopy时
[self.cloneArray addObject:[[self.originalArray objectAtIndex:i] mutableCopy]];
Run Code Online (Sandbox Code Playgroud)
我得到了相同的结果.我很迷惑.
ArrayClass.h
@interface ArrayClass : NSObject {
NSMutableArray *_originalArray;
NSMutableArray *_cloneArray;
}
@property (nonatomic, retain) NSMutableArray *originalArray;
@property (nonatomic, retain) NSMutableArray *cloneArray;
Run Code Online (Sandbox Code Playgroud)
ArrayClass.m
@synthesize originalArray = _originalArray;
@synthesize cloneArray = _cloneArray;
_originalArray = [[NSMutableArray alloc] initWithObjects: @"one", @"two", @"three", @"four", @"five", nil];
_cloneArray = [[NSMutableArray alloc] initWithCapacity:[self.originalArray count]];
for (int i=0; i<5; i++) {
[self.cloneArray addObject:[self.originalArray objectAtIndex:i]];
}
// make change to the first element of the clone array …Run Code Online (Sandbox Code Playgroud) nsmutablearray ×10
objective-c ×6
arrays ×3
ios ×3
iphone ×2
swift ×2
uitableview ×2
xcode ×2
json ×1
nscopying ×1
shallow-copy ×1
sorting ×1