ivar的属性,指向NSStrings的二维指针数组(property for ivar that points to two-dimensional array of pointers to NSStrings)
我想创建一个包含NSStrings指针的动态二维c数组的类。 我知道我可以使用包含多个NSArray的NSArray来模拟二维数组,但是如果可能的话我想使用传统的二维c数组来做这个。 除非使用“__unsafe_unretained”,否则ARC不允许将指向NSString的指针简单分配给c数组的元素:
@interface NumberStringsArray : NSObject { @public NSString * __unsafe_unretained **_array; }
为了避免内存泄漏并给出分配给c数组的每个NSString的类所有权中的对象,我将每个NSString对象的指针添加到NSMutableArray。 In - (void)dealloc我释放了获取的内存以创建二维c数组。
这是我的问题:如何基于_array ivar声明属性,以便我可以将数组的i,j元素称为“foobar.array [i] [j]”而不是“foobar-> array [i ] [J]“?
后来放大:除了__bridge之外,我以与回答者非常相似的方式做到了。 我不知道这是否有所作为。 我在这里分配二维数组:
self->_array = (NSString * __unsafe_unretained **)calloc(_columnCount, sizeof(void *)); if (!self->_array) return nil; for (UINT16 i = 0; i < _columnCount; i++) { self->_array[i] = (NSString * __unsafe_unretained *)calloc(_rowCount, sizeof(void *)); if (!self->_array[i]) { for (UINT16 a = 0; a < _columnCount; a++) if (self->_array[a]) free(self->_array[a]); if (self->_array) free(self->_array); return nil; } }
我使用从逗号分隔值文件生成的子字符串将指向NSString对象的指针放入数组中:
NSArray *numbers = [line componentsSeparatedByString: @","]; for (UINT16 i = 0; i < _columnCount; i++) { NSString *number = @""; if (i < [numbers count]) number = [numbers objectAtIndex: i]; // // save it in owners // [self.owners addObject: number]; self->_array[i][j] = number; }
在 - (void)dealloc我释放所有内存:
-(void)dealloc { for (UINT16 i = 0; i < self.columnCount; i++) if (self->_array[i]) free(self->_array[i]); if (self->_array) free(self->_array); }
I want to create a class that contains a dynamic, two-dimensional c-array of pointers to NSStrings. I know I can simulate a two-dimensional array using an NSArray containing multiple NSArrays, but if possible I'd like to do this using a traditional two-dimensional c-array. ARC won't allow a simple assignment of a pointer to an NSString to an element of a c-array unless you use "__unsafe_unretained":
@interface NumberStringsArray : NSObject { @public NSString * __unsafe_unretained **_array; }
To avoid memory leaks and to give an object in the class ownership of each NSString assigned to the c-array, I add a pointer to each NSString object to an NSMutableArray. In -(void)dealloc I free the memory acquired to create the two-dimensional c-array.
Here's my question: How do I declare a property based on the _array ivar so that I can refer to the i,j element of the array as "foobar.array[i][j]" rather than "foobar->array[i][j]"?
Later amplification: I did it in a very similar manner to the answerer except for the __bridge stuff. I don't know if that makes a difference. I allocate the two-dimensional array here:
self->_array = (NSString * __unsafe_unretained **)calloc(_columnCount, sizeof(void *)); if (!self->_array) return nil; for (UINT16 i = 0; i < _columnCount; i++) { self->_array[i] = (NSString * __unsafe_unretained *)calloc(_rowCount, sizeof(void *)); if (!self->_array[i]) { for (UINT16 a = 0; a < _columnCount; a++) if (self->_array[a]) free(self->_array[a]); if (self->_array) free(self->_array); return nil; } }
I put pointers to the NSString objects into the array using substrings generated from a file of comma-separated values:
NSArray *numbers = [line componentsSeparatedByString: @","]; for (UINT16 i = 0; i < _columnCount; i++) { NSString *number = @""; if (i < [numbers count]) number = [numbers objectAtIndex: i]; // // save it in owners // [self.owners addObject: number]; self->_array[i][j] = number; }
In -(void)dealloc I free all the memory:
-(void)dealloc { for (UINT16 i = 0; i < self.columnCount; i++) if (self->_array[i]) free(self->_array[i]); if (self->_array) free(self->_array); }
原文:https://stackoverflow.com/questions/13735092
最满意答案
这是我所知道的最佳解决方案:
[HttpPut] [ActionName("Index")] public ActionResult IndexPut(Home home) { ... }
基本上,
ActionNameAttribute
是为了处理这些场景而创建的。This is the best solution that I know of:
[HttpPut] [ActionName("Index")] public ActionResult IndexPut(Home home) { ... }
Basically the
ActionNameAttribute
was created to deal with these scenarios.
相关问答
更多-
在类模式中使用控制器时,将其他参数注入操作?(Inject other parameters to an action when using controllers in classes pattern?)[2021-09-03]
您正在寻找类中的控制器 : $app->get('/', 'Igorw\\Foo::bar'); use Silex\Application; use Symfony\Component\HttpFoundation\Request; namespace Igorw { class Foo { public function bar(Request $request, Application $app) { ... } } } You are loo ... -
具有不同Http方法的RESTful控制器,但参数相同(RESTful Controllers with Different Http Methods, But the Same Parameters)[2023-11-21]
这是我所知道的最佳解决方案: [HttpPut] [ActionName("Index")] public ActionResult IndexPut(Home home) { ... } 基本上, ActionNameAttribute是为了处理这些场景而创建的。 This is the best solution that I know of: [HttpPut] [ActionName("Index")] public ActionResult IndexPut(Home home) ... -
实际上你没有任何问题期待Logic问题。 您的Controller没有以正确的方式设计。 你需要这样的东西 //List all subjects public function index() { return view('reservation.form')->with('subjects', Subject::with('sections')->get()); } //List a single subject //If i didn't misunderstood you, you can ...
-
路由restful控制器时handleRoutingException错误(handleRoutingException error when routing restful controllers)[2024-01-20]
RESTful控制器方法需要以HTTP VERB为前缀,例如get和post 。 例: class userController extends BaseController { public function getIndex() { return "I am echoing only when you GET me!"; } public function postIndex() { return "I am echoing only when you POST to me ... -
您应该更改application/controllers/account.php public function action_welcome($name, $place) 至 public function get_welcome($name, $place) 因为Account_Controller从Base_Controller类继承$restful = TRUE ,所以使action_ -prefixed函数名不可用。 此外,您必须将account.php所有函数前缀更改为get_ ,原因相同: ...
-
多个控制器具有相同的URL路由但不同的HTTP方法(Multiple controllers with same URL routes but different HTTP methods)[2023-06-28]
UPDATE 根据您的评论,更新的问题和此处提供的答案 多个控制器类型具有相同的路由前缀ASP.NET Web Api 可以通过应用于控制器动作的HTTP方法的自定义路由约束来实现期望的结果。 检查默认的Http {Verb}属性,即[HttpGet] , [HttpPost]和RouteAttribute ,顺便说一下,我意识到他们的功能可以组合成一个类,类似于它们在Asp.Net中的实现方式-核心。 以下是GET和POST,但为其他HTTP方法PUT, DELETE...etc应用于控制器创建约束应该不 ... -
默认参数选项应该除了php中的一个次要且非常常见的未被捕获的错误。你在条件中使用赋值运算符。 使用 if($id == NULL) 代替 if($id = NULL) 或者如果你喜欢 if(is_null($id) ) 动态类型语言的悲剧之一。 让我更频繁地接受我的承认。 The default parameter option should work aside from a minor and very commonly uncaught error in php.. You're using ...
-
我经常与自己进行这场辩论,我认为我通常喜欢选项#1。 作为一种最佳实践,我们通常会努力寻找瘦的控制器,并寻找将初始化/创建逻辑分解为模型的方法。 在一个完美的世界中,操作中的大多数逻辑都是视图特定的渲染/重定向逻辑,您将为选项#2分支。 我也觉得不同观点的要求会随着时间的推移而出现分歧:“我们希望在此页面上显示不同的Flash消息”。 对于选项#2来说,这只是更多的分支。 在考虑了对模型合理的任何初始化逻辑并且可能在控制器中使用公共的before_filter之后,选项#1可能会感觉非常干净和干燥。 I o ...
-
成功! 我现在是将这些不优雅的内部类从main.js转移到它们所属的服务中的方式的一个很好的比例。 关键实现,我在阅读文档时遗漏了但在“ 我希望我知道然后我现在知道什么 - 使用AngularJS生活 ”中重申了这一点,即服务只是与依赖注入一起工作的单例。 我的服务定义如下(其中“Popup”是另一个管理错误消息弹出窗口的服务): app.factory("ThingTree", function (Popup, $q, $http) { // Database information. This ...
-
听起来你的行为与基于哪个标题的行为略有不同。我会尝试尽可能地区分差异。 例如,如果应用程序逻辑相同,但唯一的区别在于如何呈现对用户的响应,您可以考虑编写一个自定义ActionResult,它根据Http头执行不同的操作。 但是,如果逻辑完全不同,则可以实现附加到每个路由的自定义路由约束(IRoutConstraint)。 看一下HttpMethodConstraint实现的想法。 It sounds like you have slightly different behavior from your ac ...